java

位置:IT落伍者 >> java >> 浏览文章

JAVA - 线程之wait(), notify()实例


发布日期:2018年04月04日
 
JAVA - 线程之wait(), notify()实例
package introthread;

class Test {

public synchronized void w() {

Systemoutprintln(go into w

try {

wait(

} catch (InterruptedException e) {

// TODO Autogenerated catch block

eprintStackTrace()

}

Systemoutprintln(get out w

notify()

}

public synchronized void n() {

Systemoutprintln(go into n

try {

wait()

} catch (InterruptedException e) {

// TODO Autogenerated catch block

eprintStackTrace()

}

Systemoutprintln(get out n

}

}

class ThreadA extends Thread {

Test t = null;

public ThreadA(Test t) {

thist = t;

}

public void run() {

tw()

}

}

class ThreadA extends Thread {

Test t = null;

public ThreadA(Test t) {

thist = t;

}

public void run() {

tn()

}

}

public class ThreadTest {

public static void main(String[] args) {

Test t = new Test()

ThreadA a = new ThreadA(t)

ThreadA a = new ThreadA(t)

astart()

astart()

}

}

结果

开始输出

go into w

go into n

秒后输出

get out w

get out n               

上一篇:Java线程池如何体现自己的用途

下一篇:java多线程小结