| Author |
threads question in examLab practice test 1
|
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
I am really sorry as i am posting such lengthy problems but please help me to understand this code
I know you will have to write lots of explanation but please help me
it is not the case that I have not tried but I am not able to understand
and the ExamLab explanation was not enough for me
here is the code
The anser is code compiles but no output and program does not terminate
|
SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
|
 |
Larry Olson
Ranch Hand
Joined: Feb 03, 2009
Posts: 142
|
|
Prasad Kharkar wrote:I am really sorry as i am posting such lengthy problems but please help me to understand this code
I know you will have to write lots of explanation but please help me
it is not the case that I have not tried but I am not able to understand
and the ExamLab explanation was not enough for me
Hi,
I don't blame you for the question. Don't be apologetic. I assume most people taking ExamLab are in the same boat. These questions are really tough to crack. IMO this particular code is not trivial and it needs a lot of analysis and insight to be able to understand and answer. If I have the time and patience to do the analysis, I shall post my findings. Otherwise I am sure there are experts out here who will help!
Good luck
|
 |
dipayan chatterjee
Ranch Hand
Joined: Oct 03, 2007
Posts: 47
|
|
Hi Prasad,
In this program you have two threads , main thread and b. After starting b thread , main goes to sleep.
Now b thread get chance to execute . It calls run method and doTask2() method , where it acquires lock of object a
and joins main thread . Due to this, thread b goes in block state with lock of object a .
Now when main thread come out of sleep state . It try to call doTask1() method on object a , but because this method is synchronized , it needs lock of object a , but lock is not available, because it is with thread b.
So both threads now in block state.
Hope it will help you
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
dipayan chatterjee wrote:Hi Prasad,
In this program you have two threads , main thread and b. After starting b thread , main goes to sleep.
Now b thread get chance to execute . It calls run method and doTask2() method , where it acquires lock of object a
and joins main thread . Due to this, thread b goes in block state with lock of object a .
Now when main thread come out of sleep state . It try to call doTask1() method on object a , but because this method is synchronized , it needs lock of object a , but lock is not available, because it is with thread b.
So both threads now in block state.
Hope it will help you
Great, Exactly.
Try this code :
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
very well replied dipayan
nice work
thanks for that
thread resolved
|
 |
 |
|
|
subject: threads question in examLab practice test 1
|
|
|