• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non synchronized threads  RSS feed

 
Pal Sudarshan
Ranch Hand
Posts: 52
  • 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
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!