当时写的时候没写出来, 结束了改了改代码发现实现了,思路没错,写的地方写错了. 补充下线程交替输出的代码: static class Resource { int num; public Resource(int num) { this.num = num; } public synchronized void increment() { num++; if (num == 11) { num = 1; } notify(); System.out.println(num + ": " +Thread.currentThread().getName()); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Resource resource = new Resource(0); new Thread(() -> { while (true) { resource.increment(); } }, "t1").start(); new Thread(() -> { while (true) { resource.increment(); } }, "t2").start(); }
点赞 评论

相关推荐

黑皮白袜臭脚体育生:简历统一按使用了什么技术实现了什么功能解决了什么问题或提升了什么性能指标来写会更好
点赞 评论 收藏
分享
神哥不得了:首先我就是在成都,成都的互联网格外的卷,如果是凭现在的简历的话很难找到大厂,建议再添加一个高质量的项目上去,另外专业技能的话最好是超过每一条的一半
点赞 评论 收藏
分享
牛客网
牛客企业服务