Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

more wait() and notify() - deadlock situation

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm messing about with the Thread examples from the Sierra and Bates book.

I've taken the simple wait() and notify() example from page 749 and doubled up the calls to Thread B. I'm getting a deadlock situation. Can someone explain why this is, as I have created 2 separate instances of Thread B and synchronized on each separately.

class Main {
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.setName("b");
b.start();
ThreadB c = new ThreadB();
c.setName("c");
c.start();

synchronized (c) {
try {
out.println("Waiting for c to complete..."
+ Thread.currentThread().getName());
c.wait();
} catch (InterruptedException e) {
}
}
synchronized (b) {
try {
out.println("Waiting for b to complete..."
+ Thread.currentThread().getName());
b.wait();
} catch (InterruptedException e) {
}
}
out.println("Total is: " + b.total);
out.println("Total is: " + c.total);
}
}

class ThreadB extends Thread {
int total;

public void run() {
synchronized (this) {
out.println("ThreadB " + Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
out.println("Running total " + total);
total += i;
}
notify();
}
}
}

Here's some sample output:

Waiting for c to complete...main
ThreadB b
Running total 0
Running total 0
Running total 1
Running total 3
Running total 6
Running total 10
Running total 15
Running total 21
Running total 28
Running total 36
ThreadB c
Running total 0
Running total 0
Running total 1
Running total 3
Running total 6
Running total 10
Running total 15
Running total 21
Running total 28
Running total 36
Waiting for b to complete...main


Thanks
Paul
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul! welcome to javaranch.

Your code is not going into a deadlock. The problem is that when main starts waiting on b, the thread b has already completed by that time. So the notify call will not wake up the main method so main will wait to be notified forever...
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at your sample output... By the time you are waiting for the notification (on b). Thread b has long finished, and sent the notification already.

A notification that is sent, with no thread waiting, is simply discarded. So, you are waiting for a notification that was already discarded.

[EDIT: beaten to the answer again ... ]

Henry
[ December 03, 2008: Message edited by: Henry Wong ]
 
Paul Kemp
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, right, the code already got to notify() before it had even started to wait(). :roll:

I'm still trying to get my head around threading...you may have noticed. Thanks to both.
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry you were delayed by just a minute or so
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic