加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

java多线程――线程间的通讯

发布时间:2020-12-13 20:07:46 所属栏目:PHP教程 来源:网络整理
导读:public class ThreadDemo3 { public static void main(String[] args) { Resource res = new Resource(); Input input = new Input(res); Output output = new Output(res); Thread t1 = new Thread(input); Thread t2 = new Thread(output); t1.start(); t2
public class ThreadDemo3 { public static void main(String[] args) { Resource res = new Resource(); Input input = new Input(res); Output output = new Output(res); Thread t1 = new Thread(input); Thread t2 = new Thread(output); t1.start(); t2.start(); } } class Resource { String name; String gender; } class Input implements Runnable { Resource res; Input(Resource res) { this.res = res; } public void run(){ int x = 1; while (true){ synchronized (Resource.class) { if (x%2 == 0){ this.res.name = "Stephen Curry"; this.res.gender = "male"; } else { this.res.name = "斯蒂芬 库里"; this.res.gender = "男人"; } x++; } } } } class Output implements Runnable { Resource res; Output(Resource res){ this.res = res; } public void run(){ while(true){ synchronized (Resource.class) { System.out.println(this.res.name + "------" + this.res.gender); } } } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读