• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Dan's thread question....

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the possible results:
X0Y1X2Y3X4Y5
X0X1X2Y3Y4Y5
what is exactly the Thread.yield() doing in method getI() ?
Calling yield() will suggest the scheduler to move the current running thread to the runnable state, and move another one (or even the same) from runnable to running. but in this code I'm a bit confused on its usage..
I thought that since this method is synchronized, t1 or t2 will execute first (up to the scheduler which goes first), finish executing getI(), release the lock, the other thread will grab the lock and do its job. So my first guess with this question was:
X0Y1X2Y3X4Y5
Y0X1Y2X3Y4X5
but again.. don't know why yield() is there... I also thought that it was unpredictable, coz we never know how yield() behaves..
anyone willing to show me some light?
[ June 24, 2003: Message edited by: Andres Gonzalez ]
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres,

Calling yield() will suggest the scheduler to move the current running thread to the runnable state.


The yield() method is static and causes the current running thread to release the lock. I hope that helps in some way, Dave.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres
Yes you are correct yield() method won't do much here.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait a sec, yeild does not release the lock. The documentation says the thread is "paused". Only wait will release the lock (someone please correct me if I'm wrong)
The choices to the question is:


a. Prints: X0Y1X2Y3X4Y5.
b. Prints: X0X1X2Y3Y4Y5.
c. Prints: X0Y0X1Y1X2Y2.
d. Prints: X0X1X2Y0Y1Y2.
e. Compiler error.
f. Run time error.
g. None of the above.


You can't have X1Y1 or similiar because the synchronized method will prevent that from happening.
Actually, if yield did release the lock, then MAYBE the values obtained could be X1Y1, such as if the thread yields after the local variable is set but before the member variable is incremented. Nasty. Maybe that was the intended test.
[ June 24, 2003: Message edited by: Brian Joseph ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave
The yield() method is indeed static but it doesn't relases any locks.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Only wait will release the lock (someone please correct me if I'm wrong)


An object�s lock is only released by the end of a syncronized code block or by the wait method.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys...

You can't have X1Y1 or similiar because the synchronized method will prevent that from happening.


Agree 100%, but what about:
X0Y1X2Y3X4Y5
Y0X1Y2X3Y4X5
Why can't it start with Y? According to the answer, it always starts with X. Who is ensuring me that? The scheduler might say: "Ok, I have 2 threads, t1 and t2, but just because I love t2 I move this thread to running and leave t1 in the runnable state.


Actually, if yield did release the lock, then MAYBE the values obtained could be X1Y1, such as if the thread yields after the local variable is set but before the member variable is incremented.


Exactly, and this is my confusion. I would say that the result is unpredictable.. yield might/might not occur.
correct me if I'm wrong.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres

Exactly, and this is my confusion. I would say that the result is unpredictable.. yield might/might not occur.


Yield would occur and wouldn't be of much use because in this case there is no one else other than the currently executing thread that can have the control, so the control comes back to the current thread. The result may start with Y0 as well. But who's saying that it can't.
[ June 24, 2003: Message edited by: Anupam Sinha ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The result may start with Y0 as well. But who's saying that it can't.


the answer itself! . So are we saying here that Mr. Dan Chisholm's question is unpredictable? if you check the possible solutions none of them starts with Y.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres
If the possible answers doesn't states that the answer may also start with a Y0 it doesn't means that it won't it only means given the choices what would be the output. Ofcourse there might be many other possible outputs as well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic