Home Answers Viewqa Java-Beginners notify and notifyAll methods

 
 


uma
notify and notifyAll methods
2 Answer(s)      4 years and 8 months ago
Posted in : Java Beginners

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









Related Pages:
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
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
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
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
Thread method
Thread method  What is the purpose of the wait(), notify(), and notifyAll() methods
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
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
(help me) use wait() and notify() method in Thread.
(help me) use wait() and notify() method in Thread.   we have... is Thread Sintok class...we have to use wait(); and notify(); to comes out...("SINTOK"); value = false; notify(); return val; } synchronized void put(int val
java threads - Java Interview Questions
java threads  what is difference between the Notify and NotifyAll
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
Java Thread - Java Beginners
. currentThread() method 2. notify(), notifyAll() methods 3. execution... are the links where you can find very good examples of wait(), notify(), currentThread() and notifyAll() methods. Please visit these links to get more detailed
java - Java Interview Questions
the notify() method or the notifyAll() method for this object... the notify() method or the notifyAll() method for this object, or some other...java  what are the methods in objects class  Hi friend
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
native methods
native methods  what is native methods in java?   A native method is a method that is implemented in a language other than Java. The Java native method is a great way to gain and merge the power of C or C++ programming
java - Java Interview Questions
java  why notify() and notifyAll() methods are written in Object class not in Thread class?  Hi Friend, The locking mechanism happens at the Object level so all these methods are in Object Class. Java
validate() and reset() methods
validate() and reset() methods   Describe validate() and reset() methods
Various methods of httpservletresponse interface
Various methods of httpservletresponse interface  What are the various methods of httpservletresponse interface
Final Methods - Java Tutorials
methods The final method  can be declare as follows: public final String
methods type - Java Beginners
methods type in Java  Give me an example programs of methods types in Java
Cookie methods in jsp
Cookie methods in jsp   Define cookie methods in jsp ?    Cookie methods : clone() getComment() getDomain() getMaxAge() getName() getPath() getSecure() getValue() getSecure() getVersion
GET and POST methods
GET and POST methods   What are the differences between GET and POST methods in form submitting, give the case where we can use GET and we can use POST methods
Java overloaded methods
Java overloaded methods  Can overloaded methods can also be overridden
factory methods in java?
factory methods in java?  what are factory methods in java?   Hi Friend, Factory methods are static methods that return an instance of the native class like Pattern.compile(), Calendar.getInstance
abstract methods in java
abstract methods in java  what is abstract methods in java.give better examples for understanding   Hi Friend, Please visit the following link: http://www.roseindia.net/java/master-java/abstract-class.shtml Thanks
to create a java class and methods
it with methods that can be used to reverse a list & append two lists.Also to comment on whether the dsign ade has led to make methods for append
Callback Methods
Callback Methods       Callbacks methods are the way of managing life cycle of an instance. Callback methods are generally used by containers. The methods are called at specific time
Methods in Jsp - Development process
Methods in Jsp   Hi, My interviewer said we should declare & define all methods inside _jspService() method only. is it correct . Thanks Prakash  Hi Friend, Yes, all the methods should be declared and defined
looping through a list of methods
looping through a list of methods  I have a number of methods that do the almost same thing: public void setColorRed (int colorvalue); public...); Is there a way to place these methods in a list and then call each one from
methods in the applet - Applet
methods in the applet  import java.awt.*; import java.applet.Applet... provides the Applet class with default implementation for all the applet methods. You can implement these methods in the applet class when you want to override
Overloaded methods - Java Beginners
Overloaded methods  Write two overloaded methods that return the average of an array with the following headers: a) public static int average(int[] array) b) public static double average(double[] array)   Hi Friend
Methods - OOP
Java NotesMethods - OOP Static methods If your method doesn't use... utility methods and main(...), you should probably not be using static methods. A good example of a static methods in Java is the Math or Character
Methods in Java - Java Beginners
Methods in Java  Hello. Currently i am involved in a group project and we have each been given a specific part of code to create our joint programI however have been given the job to create a method for storing and recalling 5
java - Java Interview Questions
java  why notify() and notify() methods are written in Object class not in Thread class
HTML form methods GET and POST
HTML form methods GET and POST  What is the difference between the HTML form methods GET and POST
The ActionForm and what are important methods in ActionForm.
The ActionForm and what are important methods in ActionForm.  What is the ActionForm and what are important methods in ActionForm

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.