Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
Question 16, Practice Exam from K&B Book
Konrad Till
Greenhorn
Posts: 7
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can anybody explain me, why does this code print also "0"?
(the code is slightly modified by me - added the println statement)
It looks like 0 > 0 returns true. I'm quite confused about that.
public class Stubborn implements Runnable{ static Thread t1; static int x = 5; public void run(){ if(Thread.currentThread().getId() == t1.getId()) shove(); else push(); } static synchronized void push(){shove();} static void shove(){ synchronized(Stubborn.class){ System.out.print(x-- + " "); try{ Thread.sleep(2000); } catch (Exception e) {;} System.out.println("\nbefore if statement, x is: " + x); if(x > 0) push(); } } public static void main(String [] args){ t1 = new Thread(new Stubborn()); t1.start(); new Thread(new Stubborn()).start(); } }
dennis deems
Ranch Hand
Posts: 808
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Try printing the current
thread
Id just before x is printed, and see if that helps to clear things up.
Konrad Till
Greenhorn
Posts: 7
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks! I totally forgot about that statement ;)
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Please suggest ways to cause deadlock in this code
Why won't the following Deadlock? Exam 2 q.16
Thread Deadlocking doubt
Why does this count down to zero and not to 1
Threading Problem
More...