黑马程序员技术交流社区
标题:
基础学习day12多线程1
[打印本页]
作者:
不怕黑人
时间:
2015-7-22 22:16
标题:
基础学习day12多线程1
public class Day12Test {
public static void main(String[] args) {
Res r = new Res();
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
class Res{
private String name;
private String sex;
private boolean flag = false;
public synchronized void set(String name,String sex){
if (flag)
try{this.wait();}catch(Exception e){}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out(){
if (!flag)
try{this.wait();}catch(Exception e){}
System.out.println(name+">>>"+sex);
flag = false;
this.notify();
}
}
class Input implements Runnable{
private Res r;
Input(Res r){
this.r = r;
}
public void run(){
int x = 0;
while (true){
if (x==0)
r.set("jack", "man");
else
r.set("丽丽", "女女女女");
x=(x+1)%2;
}
}
}
class Output implements Runnable{
private Res r;
Output(Res r){
this.r = r;
}
public void run(){
while(true){
r.out();
}
}
}
复制代码
作者:
康恒强
时间:
2015-7-22 22:31
好复杂,,
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2