没有合适的资源?快使用搜索试试~ 我知道了~
首页Java线程中wait,await,sleep,yield,join用法总结.pdf
资源详情
资源评论
资源推荐

一、wait()、notify()、notifyAll()用法
• obj.wait()/obj.wait(long timeout)是Object中的方法,当线程调用wait()方法,当前
线程释放对象锁,进入等待队列。
• obj.notify()/obj.nogifyAll()是Object中的方法,唤醒在此对象上wait()的单个或者
所有线程。
测试代码:
public class ThreadWaitNotify {
public static void main(String[] args) throws InterruptedException {
//创建一个线程池
ExecutorService executorService = Executors.newCachedThreadPool();
//创建DemoTest对象
DemoTest demoTest = new DemoTest();
//用线程池创建线程异步执行waitTest方法
executorService.submit(() -> demoTest.waitTest());
//用线程池创建线程异步执行notifyTest方法
executorService.submit(() -> demoTest.notifyTest());
}
//测试wait和notify测试demo
static class DemoTest {
//唤醒线程
public synchronized void notifyTest() {
System.out.println("方法notifyTest开始执行了");
notify();
System.out.println("方法notifyTest执行结束了");
}
//执行wait操作将线程挂起,注意必须结合synchronized使用
public synchronized void waitTest() {
System.out.println("方法waitTest开始执行了");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("方法waitTest执行结束了");
}
}
}
打印日志:
方法waitTest开始执行了
方法notifyTest开始执行了

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0