Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads question in examLab practice test 1

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very well replied dipayan
nice work
thanks for that
thread resolved
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic