Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners notify and notifyAll methods
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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



Advertisement
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 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
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
Advertisements
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
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
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
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
(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
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
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
Various methods of httpservletresponse interface
Various methods of httpservletresponse interface  What are the various methods of httpservletresponse interface
validate() and reset() methods
validate() and reset() methods   Describe validate() and reset() methods
Final Methods - Java Tutorials
methods The final method  can be declare as follows: public final String
Servlet Methods
Servlet Methods In this section we will read about the various methods... methods that are used to initialize a Servlet, handles the request received..., the Servlet methods are called life-cycle methods of Servlet. Following
methods type - Java Beginners
methods type in Java  Give me an example programs of methods types in Java
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
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
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
Java overloaded methods
Java overloaded methods  Can overloaded methods can also be overridden
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
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
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
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
Advertisements
 

 

 

DMCA.com