How can we achieve communication between two threads?
How can we achieve communication between two threads?
Using shared objects.
Originally Posted by realneeraj
can you be more elaborate?
For example, in producer-consumer problem, we use a common object, the producer will write in the common object and the consumer will read from it. If you intend to pass some data between two threads then you can use some common object between the two. For example
class CommonObj {
String data;
void put(String s) {
data=s;
}
String get() {
return data;
}
}
You can pass an instace of the CommonObj class to each thread. Any thread can put some thing in the common object and other one can get it. Ofcourse you need to take care of synchronization issues.
using wait(),notify(),notifyAll() methods
import java.awt.*;
import java applet.*;
public class oval extends Applet
{
public static void paint (Graphics g)
{
g.drawoval(100,56);
g.setcolor(98,33);
g.filloval(98,23);
}
}
Can any you Please Tell by code?