package com.hp.concurrency;
public class SignalTest {
public static final Object lock = new Object();
public static void main(String[] args) {
new TestThread().start();
testRun();
}
public static void testRun() {
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized (SignalTest.lock) {
try {
System.out.println("Main got SignalTest.lock ");
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SignalTest.lock.notify();
}
System.out.println("testRun over");
}
}
class TestThread extends Thread {
public void run() {
synchronized (SignalTest.lock) {
System.out.println("TestThread got SignalTest.lock ");
try {
Thread.sleep(4000);
//SignalTest.lock.wait();
SignalTest.lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("run over");
}
}
Java 同步信号使用
最新推荐文章于 2025-03-31 19:07:06 发布