Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee 朱秋贵内科诊所 My腾云code

多线程 input生产 res(username,sex) out消费

public class Thread001 {

class Res{
public String userName;
private char sex;
}

class InputThread extends Thread{

Res res;
public InputThread(Res res) {
this.res = res;
}

@Override
public void run() {
int count=0;
while (true){

if(count==0){
res.userName="张胜军";
res.sex='男';
}else {
res.userName="小薇";
res.sex='女';
}
count=(count+1)%2;
}

}
}


class OutThread extends Thread {

Res res;

public OutThread(Res res) {
this.res = res;
}

@Override
public void run() {
while (true){
System.out.println(res.userName+","+res.sex);
}
}
}

public static void main(String[] args) {
new Thread001().start();
}

public void start(){
Res res=new Res();
new InputThread(res).start();
new OutThread(res).start();
}

}





posted @ 2021-04-03 21:11  cn2024  阅读(55)  评论(0编辑  收藏  举报