java 多线程的唤醒

 package TestThread.ThreadSynchronized.TestInterruptedException;

 public class InterruptDemo {
public static void main(String[] args) {
TestWait t = new TestWait("线程1");
TestWait t1 = new TestWait("线程2");
Test te = new Test("线程3", t);
t.start();
t1.start();
te.start();
}
} class TestWait extends Thread {
String name; public TestWait(String name) {
super(name);
this.name = name;
} public synchronized void run() {
for (int i = 0; i < 5; i++) {
if (i == 4) {
try {
// 等待之后立即释放当前锁,并且进入等待池中等待唤醒
// 当等待池中的线程被唤醒后,再次执行此语句之后的语句
this.wait();
System.out.println(Thread.currentThread().getName() + ":我还没有被执行到!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ":当前的值为--->" + i);
}
}
} class Test extends Thread {
private TestWait testwait;
String name; public Test(String name, TestWait testwait) {
super(name);
this.name = name;
this.testwait = testwait;
} public void run() {
synchronized (testwait) {
testwait.notify();// 调用此方法后,指定的对象将被唤醒,唤醒之后将继续执行后面的操作
}
}
}

执行的结果为:

java 多线程的唤醒

上一篇:vue报错 ModuleBuildError: Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.


下一篇:CentOS 6.8 安装 RabbitMQ