File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B: Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B: Threads" Watch "Doubt in K&B: Threads" New topic
Author

Doubt in K&B: Threads

Tomas Jansson
Greenhorn

Joined: Sep 29, 2006
Posts: 5
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

Joined: May 02, 2006
Posts: 218
Not everyone is going to have access to the book here. Would be great if you posted the code!


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Tomas Jansson
Greenhorn

Joined: Sep 29, 2006
Posts: 5
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

Joined: Sep 06, 2004
Posts: 26
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?


-Raj
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
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

Joined: Sep 06, 2004
Posts: 26
does it mean main thread will not be locked forever.
Tomas Jansson
Greenhorn

Joined: Sep 29, 2006
Posts: 5
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in K&B: Threads
 
Similar Threads
Doubt in threads
Thread Doubt
Thread Doubt
Doubt on synchronization