Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in K&B: Threads

 
Tomas Jansson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doubt of mine is dealing with the example on page 720-721. Writing code like that, isn't it possible that will cause a live lock? What if thread 'b' is finished before the main thread gets the lock on 'b'? Won't that cause 'a' to wait for 'b' to release the lock that will never released. That should be possible since we can't guarantee the order in which the threads are run. An easy way to demonstrate that is to call Thread.sleep(1000) right after thread 'b' is started.

--Tomas
[ October 05, 2006: Message edited by: Tomas Jansson ]
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not everyone is going to have access to the book here. Would be great if you posted the code!
 
Tomas Jansson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your reply. If I would have kept on reading right away I would have found the answer on the question, and I was right. I was just a little bit confused when they didn't say that right away. If you're interested in the example the code is (I added a comment where I think the thread could get stuck):

class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();
b.start();

synchronized(b) {
try {
System.out.println("Waiting for b to complete...");

b.wait(); // This could cause the main thread to wait forever

} catch (InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}

class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
notify();
}
}
}

--Tomas
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me why main thread will be locked for ever? Is it because the notify() method on thread 'b' doesn't notify 'main' thread? If so how is the main thread gets to run again? Is there a time out happening?
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Calling the wait() method on an object releases its lock and waits until it will be notified by the same object.
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it mean main thread will not be locked forever.
 
Tomas Jansson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If thread b enter the synchronized block before the main thread does it will have the lock for the object, and could therefor finish its run method before the main thread reach the wait() statement. The thing is that when b execute the notify() statement and no thread is waiting no thread will be notified. So when the main thread reach the wait() statement it will be waiting for a notify that has already happend and that is just missed.

One simple way to illustrate this is to add a Thread.sleep(1000) just after b.start(). This will cause b to finish before the main thread reach wait().

One way to prevent this is to have a check like this in the main thread.
while(!(b.isDone())
b.wait

This will make the main thread just to wait when b isn't done. (b.isDone() is up to you to write). Note the "while" instead of "if", this is to make sure that b is done when the main thread is woke up.

--Tomas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic