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
Win a copy of
Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework
this week in the
Java in General
forum!
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
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Programmer Certification (OCPJP)
Question 16, Practice Exam from K&B Book
Konrad Till
Greenhorn
Posts: 7
posted 12 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 12 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 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks! I totally forgot about that statement ;)
Men call me Jim. Women look past me to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
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...