Tomas Jansson

Greenhorn
+ Follow
since Sep 29, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tomas Jansson

How hard could it be?

This calls for
16 years ago
Hello everyone!
I was studying for the exam and was surprised that the following problem was not in the SCJP book:


As this code demonstrates the run method of ThreadTest will be waiting for ever. How is this problem resolved? Is there another way than using the timer in the wait statement?

--Tomas
[ May 20, 2007: Message edited by: Tomas Jansson ]
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
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
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 ]