notify and notifyAll methods

Ads

 
 

Share on Google+Share on Google+

uma
notify and notifyAll methods
2 Answer(s)      9 years ago
Posted in : Java Beginners
Ads
View Answers

September 29, 2008 at 2:22 PM




September 29, 2008 at 4:44 PM


Hi friend,


import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Notify extends Object {
private List list;

public Notify() {
list = Collections.synchronizedList(new LinkedList());
}

public String rItem() throws InterruptedException {
synchronized (list) {
while (list.isEmpty()) {
print("wait()");
list.wait();
print("done with wait()");
}
String item = (String) list.remove(0);

return item;
}
}

public void itemInert(String item) {
print("entering");
synchronized (list) {
list.add(item);
print("added: '" + item + "'");

list.notifyAll();
print("notified");
}
print("leaving");
}

private static void print(String msg) {
String name = Thread.currentThread().getName();
System.out.println(name + ": " + msg);
}

public static void main(String[] args) {
final Notify nf = new Notify();

Runnable r1 = new Runnable() {
public void run() {
try {
String item = nf.rItem();
print("returned: '" + item + "'");
} catch (InterruptedException ix) {
print("interrupted!");
} catch (Exception x) {
print("threw an Exception!!!\n" + x);
}
}
};

Runnable r2 = new Runnable() {
public void run() {
nf.itemInert("Hello!");
}
};

try {
Thread thread1 = new Thread(r1, "A");
thread1.start();

Thread.sleep(500);

Thread thread2 = new Thread(r1, "B");
thread2.start();

Thread.sleep(500);

Thread thread3 = new Thread(r2, "C");
thread3.start();

Thread.sleep(1000);

thread1.interrupt();
thread2.interrupt();
} catch (InterruptedException x) {
}
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
notify and notifyAll methods - Java Beginners
notify and notifyAll methods  I need a program demonstrating the methods of notify and notifyAll. please can you suggest me a way out.  ... java.util.LinkedList; import java.util.List; public class Notify extends Object
Java Thread Wait,NotifyAll
Wait notifyAll both are the methods of the object class. notify is used to notify only the first waiting thread. notifyAll notifies all the thread to resume their lock Java Thread Wait Notifyall Example class passenger
Advertisements
Object Class Methods in Java
these are as clone(), equals(), finalize(), notify(), notifyAll(), getClass(), toString...() notify() notifyAll() getClass() toString() wait() 1. finalize... void notify() 5. notifyAll() Method:-  The notifyAll method has all
Why the wait(),notify() should not avaialable in Thread class.
Why the wait(),notify() should not avaialable in Thread class.  wait(),notify(),notifyall() is available in Object class.We are using those method in Threading then why don't they put the 3 methods in thread class
java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll
Java Thread Wait,Notify
Wait and notify both are the methods of the Object class. Wait makes... other thread to work by releasing its lock. Both methods are called in the synchronized context(methdod,block) Java Thread Wait & Notify Example public
What are the methods in Object? - Java Beginners
(); notify(); notifyAll(); hashCOde(); equals(); Thnaks...What are the methods in Object?   Hi, What are the methods in Object? Give example of methods in Object. Thanks   Hello
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
Version of com.dorkbox>Notify dependency
List of Version of com.dorkbox>Notify dependency
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
Maven Dependency Notify >> 2.0
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.0 in your project
Maven Dependency Notify >> 2.1
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.1 in your project
Maven Dependency Notify >> 2.11
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.11 in your project
Maven Dependency Notify >> 2.12
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.12 in your project
Maven Dependency Notify >> 2.13
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.13 in your project
Maven Dependency Notify >> 2.14
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.14 in your project
Maven Dependency Notify >> 2.15
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.15 in your project
Maven Dependency Notify >> 2.16
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.16 in your project
Maven Dependency Notify >> 2.17
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.17 in your project
Maven Dependency Notify >> 2.18
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.18 in your project
Maven Dependency Notify >> 2.19
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.19 in your project
Maven Dependency Notify >> 2.20
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.20 in your project
Maven Dependency Notify >> 2.4
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.4 in your project
Maven Dependency Notify >> 2.5
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.5 in your project
Maven Dependency Notify >> 2.6
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.6 in your project
Maven Dependency Notify >> 2.7
You should include the dependency code given in this page to add Maven Dependency of com.dorkbox >> Notify version2.7 in your project
Version of com.dorkbox>Notify-Dorkbox-Util dependency
List of Version of com.dorkbox>Notify-Dorkbox-Util dependency
static methods
static methods  why static methods cannot read or write the instance variables
Agile methods
Agile methods   Why use Agile methods?   This methods focus on shorter iterations, in which the software is brought to a releasable level of quality fairly often, usually somewhere between weekly and monthly. Short

Ads

 
Advertisement null

Ads