Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on Thread - Is the code went in to DeadLock state ?  RSS feed

 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written the code on thread as follows,



if i comment line no. 18 , my output would be,

Thread 1 THREAD IN EXCECUTION 0
Thread 2 THREAD IN EXCECUTION 1
Thread 1 THREAD IN EXCECUTION 2
Thread 2 THREAD IN EXCECUTION 3
Thread 1 THREAD IN EXCECUTION 4
Thread 2 THREAD IN EXCECUTION 5
Thread 2 THREAD IN EXCECUTION 6
Thread 1 THREAD IN EXCECUTION 6
Thread 1 THREAD IN EXCECUTION 8
Thread 2 THREAD IN EXCECUTION 9
Thread 1 THREAD IN EXCECUTION 10
Thread 2 THREAD IN EXCECUTION 11
Thread 1 THREAD IN EXCECUTION 12
Thread 2 THREAD IN EXCECUTION 13
Thread 1 THREAD IN EXCECUTION 14
Thread 2 THREAD IN EXCECUTION 15
Thread 1 THREAD IN EXCECUTION 16
Thread 2 THREAD IN EXCECUTION 17
Thread 2 THREAD IN EXCECUTION 18
Thread 1 THREAD IN EXCECUTION 18

if i uncomment that perticular line my output will be,

Thread 1 THREAD IN EXCECUTION 0
Thread 2 THREAD IN EXCECUTION 1


and the program is stuck some where ,
i'm not able to make out whats happening , can anybody please help me out
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.. its is hanged at join() when you uncomment it..

Because.. when you execute join() on a X thread, the current thread waits till the X thread dies.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why does it hang?

t2.join() means t1 will join after t2 completes. Does not thread t2 gets chance to take the control.

Please clarify.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both threads call join() on t2, one after the other. When t2 calls join() on itself, it will just sit there forever waiting for itself to return from the run() method. Both threads execute the first time through the loop and then stop forever, each waiting for t2 to do more loops, which of course it never will.

Perhaps you meant



?
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... it makes sense. It works now when we add the lines of code suggested.


 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But its not deadlock ??? is it?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Samuel wrote:But its not deadlock ??? is it?


Yes, it's not. The classic description of a deadlock is that t1 is waiting for a resource that t2 holds, and t2 is waiting for a resource that t1 holds. In this case, I suppose you would say that t1 and t2 are both waiting for a resource that the JVM itself holds, so it's more a case of two threads both being blocked. It doesn't have that interdependency that "deadlock" normally implies.
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
friends i did something like this wether its write or wrong in my understanding please let me ,

form my above post and than reading some very very valuable discussion above i got a clue , to explain it i wrote it below,



the output of the code is

Thread 1 THREAD IN EXCECUTION 0
.....
thats it . the output goes in hung state, from which i came to know that threat says to its self to "stop and run after its own completion" , so it hungs up ,
now if i comment line no. 11 and uncomment line no. 10, than my out put will be

Thread 1 THREAD IN EXCECUTION 0
Thread 1 THREAD IN EXCECUTION 1
Thread 1 THREAD IN EXCECUTION 2
Thread 1 THREAD IN EXCECUTION 3
Thread 1 THREAD IN EXCECUTION 4
Thread 1 THREAD IN EXCECUTION 5
Thread 1 THREAD IN EXCECUTION 6
Thread 1 THREAD IN EXCECUTION 7
Thread 1 THREAD IN EXCECUTION 8
Thread 1 THREAD IN EXCECUTION 9

ie. the current thread stops for uneven time but as we have set the time , ie. 1 sec. the thread has to start again .

now in my very first code on line number 18 i just have to replace code with "t2.join(1000)" so that to output will be ...

Thread 1 THREAD IN EXCECUTION 0
Thread 2 THREAD IN EXCECUTION 1
Thread 1 THREAD IN EXCECUTION 2
Thread 2 THREAD IN EXCECUTION 2
Thread 2 THREAD IN EXCECUTION 4
Thread 1 THREAD IN EXCECUTION 5
Thread 2 THREAD IN EXCECUTION 6
Thread 1 THREAD IN EXCECUTION 7
Thread 2 THREAD IN EXCECUTION 8
Thread 1 THREAD IN EXCECUTION 9
Thread 1 THREAD IN EXCECUTION 10
Thread 2 THREAD IN EXCECUTION 10
Thread 2 THREAD IN EXCECUTION 12
Thread 1 THREAD IN EXCECUTION 13
Thread 2 THREAD IN EXCECUTION 14
Thread 1 THREAD IN EXCECUTION 15
Thread 2 THREAD IN EXCECUTION 16
Thread 1 THREAD IN EXCECUTION 16
Thread 2 THREAD IN EXCECUTION 18
Thread 1 THREAD IN EXCECUTION 18


so what you say , whether i am right or wrong
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
join(1000) gives up after one second, so yes, the program would eventually run to completion.
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, I suppose you would say that t1 and t2 are both waiting for a resource that the JVM itself holds


how come JVM is holding resource, in my very first code "t2.join()" when thread starts running and it comes to this line it is stuck with its own join so how JVM is holding a resource, can you put some light on it as i am not able to understand ......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!