This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Non synchronized threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Non synchronized threads" Watch "Non synchronized threads" New topic
Author

Non synchronized threads

Pal Sudarshan
Ranch Hand

Joined: Jun 10, 2004
Posts: 52
The code is taken directly from Dr. K's book. Mutex class creates two threads: Pusher and Popper.

MY QUESTION:
The output of the code is:
Exit from main().
Pushed: true
Popped: null
What I can't understand is why is popped null. First, I thought maybe because the JVM did a context switch in the middle of incrementing toOfStack, but then I put in System.out statements and there was no context switch during ++topOfStack.

Here is the code:
Pal Sudarshan
Ranch Hand

Joined: Jun 10, 2004
Posts: 52
the smiley face shouldn't be there:
the for loop is an infinite loop.
siddharth chhabra
Greenhorn

Joined: Mar 04, 2004
Posts: 4
I think after the ++topOfStack; since you have a thread.sleep ...the object on top of stack is still null

in the meanwhile the pop executes and assigns null to the obj which gets printed in your code


Siddharth
 
GeeCON Prague 2014
 
subject: Non synchronized threads