• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Totally lost on the Thread question from Sun's Assessment

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't even get the point of this question from sun's Assessment



Please tell me the answer and explain why. Thanks a lot
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not how JavaRanch works. We'll try to help you learn Java concepts, but you need to do the work. If you're not sure about threads, start by working through the concurrency chapter of the Sun Java Tutorial. Once you've understood what's covered there, you should have a grasp of what the question is about. If there's something specific about it that you don't understand, tell us what that is, and what you think should be happening and why. Then we'll be happy to jump in with clarifications.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my knowledge, the answer c and e are correct!
But not sure whether it is correct.

In the main() method, four threads are started with same Waiting2(Runnable) instance. And the run() method is synchronized so if one thread gets to execute run() method, others will be blocked.
Lets take 6,7,8,9(as these are only used in the answer) are the ids for the threads created.
The ID of the thread which first gets chance to execute will be printed after incrementing the value of state by 1 and it will start waiting. Like that the next two threads, which will get the chance to execute will print their id after incrementing the value of state by 1 and start waiting.
Till now three ids of different threads are printed.
And when the fourth thread gets chance to execute, if condition which checks the value of state lesser than 3 will become false and else block will get executed. There it will sleep for 2 seconds and notifies the waiting threads. There is no print stmt inside else block so the options d,a,g(which contains 4 diff ids) are eliminated.

The first three waiting threads will execute based on the JVM scheduling. We cannot say in which order they will execute. But the ids of those three threads will be printed.
So the options c and e are correct.

option f is wrong because id 6 is printed thrice.
option b is wrong because ids 7 and 8 will also be printed.

Hope this is clear!
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A short description is as follows

I think c and e are correct
beacause total 4 threads are there
3 out of them enter into if block
1 out of them enter into else block
Two println statement are there in if block
print after wait() is executed only by three threads which enters if block
The thread id printed before will be printed after wait() statement
order of execution of threads cannot be predicted

Regards
Ninad

--------------
SCJP 5.0 97%

[ September 16, 2008: Message edited by: Ninad Kulkarni ]
[ September 16, 2008: Message edited by: Ninad Kulkarni ]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fully agree with M Srilata, and a very good explanation.
Going by the options: only six numbers would be printed since the print statement is two times and each thread prints twice(The fourth thread never really prints since it enters the else part).
So option a,b,g are incorrect.
Option f is out since it prints 6 twice.
And option d is incorrect since 9 is printed which cant be.
So leaves us with two options: 'c' and 'e'.
M Srilata nails it !
[ September 16, 2008: Message edited by: Paul Somnath ]
 
Maggie Zhou
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry I didn't mean to do that. I tried to analyze it by myself. And ran serveral times to check the output. I just didn't get the point of this question. I only thought the out put will be random.

Now thanks all the asswers I know the point now.

In this specific question: the previous three thread printed their ID then move to waiting status, and the last thread to call them back. that is the point of this question. I agree with you the answer should be c and e

the reasons are:
1, the last thread won't be printed out, so we cannot get 9 print out: a d g are wrong
2, there are notifyAll(), means all the previous three thread will back and print out, and they won't be reprint twice, so there are totall 6 output: b f are wrong

Thank again
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic