线程lock condition使用

is668com is668com 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是具体例子,一直抛错,搞不懂应该怎么用了;求修改求思想
package com.sixth.exam;

import java.util.concurrent.locks.*;

public class T3{
public static void main(String[] args)
{

ReentrantLock lock=new ReentrantLock();
PrintThread pt=new PrintThread(lock);
Thread t=new Thread(pt);
t.start();
}
}
class PrintThread implements Runnable
{

ReentrantLock lock;
Condition condition;
PrintThread1 pt1;
Thread t1;
public PrintThread(ReentrantLock lock)
{
this.lock=lock;
condition=lock.newCondition();
pt1=new PrintThread1(lock);
t1=new Thread(pt1);
}
public void run()
{
t1.start();
for(int i=3;i>=0;i--)
{
System.out.println(i+"....");
if(i==1)
{
System.out.println("起床啦");
condition.signalAll();
}
}

}
}
class PrintThread1 implements Runnable
{
ReentrantLock lock;
Condition condition;
public PrintThread1(ReentrantLock lock)
{
this.lock=lock;
condition=lock.newCondition();
}
public void run()
{
try {
lock.newCondition().await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("复活了~");
}
}
...全文
评论 3
收藏 已收藏
分享
返回顶部
is668com
Java EE 67,535 成员 225,843 内容