File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non synchronized threads

 
Pal Sudarshan
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the smiley face shouldn't be there:
the for loop is an infinite loop.
 
siddharth chhabra
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic