斗者_2013的留言板

斗者_2013
Java领域优质创作者
博客专家认证
2020-01-02 06:43:35
大家好,这里是我的留言板,如果有问题,欢迎大家留言,我会第一时间进行回复
...全文
152 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky in blue 2020-05-24
  • 打赏
  • 举报
回复
你呢你帮我看看这个简单的线程吗?老是输出结果不正确
sky in blue 2020-05-24
  • 打赏
  • 举报
回复
import java.util.concurrent.*; import java.util.*; public class text777 { public static void main(String[] args)throws Exception{ Integer i=new Integer(0); P p=new P(i); C c=p.getC(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(p); exec.execute(c); Thread.sleep(20000); exec.shutdownNow(); } } class P implements Runnable//输出偶数 { Integer i; C c; public P(Integer i){ this.i=i; c=new C(this.i,this); } public C getC(){ return c; } public void run(){ while(!Thread.interrupted()){ try{ synchronized(this){ while(i%2==0) { wait(); } } synchronized(c){ System.out.println(i++); c.notifyAll(); } } catch(Exception e){throw new RuntimeException(e+"p");} } } } class C implements Runnable//输出奇数 { Integer i; P p; public C(Integer i,P p){ this.i=i; this.p=p; } public void run(){ while(!Thread.interrupted()){ try{ synchronized(this){ while(i%2==1) { wait(); } } synchronized(p){ System.out.println(i++); p.notifyAll(); } } catch(Exception e){throw new RuntimeException(e+"c");} } } }

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧