This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

wait and notify help needed  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai can any one please tell me what is wrong with this code .

public class ThreadA {

public static void main(String args[]){
ThreadA a=new ThreadA();
ThreadB b=new ThreadB();
b.start();
synchronized(ThreadB.class) {
try {
System.out.println("Waiting for b to complete...");
ThreadB.class.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}

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

}



I am getting result as

Waiting for b to complete...

the thread is in waiting for long time its not notifying the waiting thread.

Help needed .
 
Bartender
Posts: 4089
54
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code only won't even compile with

MysecondThread MysecondThread=new MysecondThread();



If we ignore that and work on the rest(if that's what you expected)

ThreadB.class.wait();



You are waiting on a class not a specific object. Try changing it to b (with synchronized(b) instead synchronized(ThreadB.class) also).
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few questions..

1> Why do you need to synchronize anything at all in this problem?
I don't see the possibility of any race conditions in the context of the problem you have stated.

2> Why do you need an instance of ThreadA in the main() method of ThreadA?

3> Also what's this MysecondThread class for?

You can achieve your intended result by invoking the Thread.join() method as described below.


public class ThreadA{
public static void main(String args[]) {
ThreadB b = new ThreadB();
b.start();
try {
System.out.println("Waiting for b to complete...");
b.join();
} catch (InterruptedException e) {
}
System.out.println("Total is: " + b.total);
// }
}
}

class ThreadB extends Thread {

int total;
public void run() {
for (int i = 0; i < 100; i++) {
total += i;
}

}


[ August 28, 2008: Message edited by: Ajay Saxena ]
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!