Java Tutorial/Thread/Wait Notify
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Monitor a thread"s status.
class MyThread extends Thread{
boolean waiting= true;
boolean ready= false;
public void run() {
String thrdName = Thread.currentThread().getName();
System.out.println(thrdName + " starting.");
while(waiting)
System.out.println("waiting:"+waiting);
System.out.println("waiting...");
startWait();
try {
Thread.sleep(10000);
} catch(Exception exc) {
System.out.println(thrdName + " interrupted.");
}
System.out.println(thrdName + " terminating.");
}
synchronized void startWait() {
try {
while(!ready){
wait();
}
} catch(InterruptedException exc) {
System.out.println("wait() interrupted");
}
}
synchronized void startWork() {
ready = true;
notify();
}
}
public class Main {
public static void main(String args[]) throws Exception{
MyThread thrd = new MyThread();
thrd.setName("MyThread #1");
System.out.println(thrd.getName()+" Alive:"+thrd.isAlive()+" State:" + thrd.getState() );
thrd.start();
Thread.sleep(50);
System.out.println(thrd.getName()+" Alive:"+thrd.isAlive()+" State:" + thrd.getState() );
thrd.waiting = false;
Thread.sleep(50);
System.out.println(thrd.getName()+" Alive:"+thrd.isAlive()+" State:" + thrd.getState() );
thrd.startWork();
Thread.sleep(50);
System.out.println(thrd.getName()+" Alive:"+thrd.isAlive()+" State:" + thrd.getState() );
if(thrd.isAlive())
System.out.println("alive");
System.out.println(thrd.getName()+" Alive:"+thrd.isAlive()+" State:" + thrd.getState() );
}
}
Pausing a Thread: set boolean a variable that the thread checks occasionally
public class Main {
public static void main(String[] argv) throws Exception {
MyThread thread = new MyThread();
thread.start();
while (true) {
synchronized (thread) {
thread.pause = true;
}
synchronized (thread) {
thread.pause = false;
thread.notify();
}
}
}
}
class MyThread extends Thread {
boolean pause = false;
public void run() {
while (true) {
synchronized (this) {
while (pause) {
try {
wait();
} catch (Exception e) {
}
}
}
}
}
}
Stopping a Thread: set a boolean variable that the thread checks occasionally
public class Main {
public static void main(String[] argv) throws Exception {
MyThread thread = new MyThread();
thread.start();
thread.stop = true;
}
}
class MyThread extends Thread {
boolean stop = false;
public void run() {
while (true) {
if (stop) {
return;
}
}
}
}
Use wait() and notify() from Object class
class MyResource {
boolean ready = false;
synchronized void waitFor() throws Exception {
System.out.println(Thread.currentThread().getName()+ " is entering waitFor().");
while (!ready)
wait();
System.out.println(Thread.currentThread().getName() + " resuming execution.");
}
synchronized void start() {
ready = true;
notify();
}
}
class MyThread implements Runnable {
MyResource myResource;
MyThread(String name, MyResource so) {
myResource = so;
new Thread(this, name).start();
}
public void run() {
try {
myResource.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String args[]) throws Exception {
MyResource sObj = new MyResource();
new MyThread("MyThread", sObj);
for (int i = 0; i < 10; i++) {
Thread.sleep(50);
System.out.print(".");
}
sObj.start();
}
}