/*
线程间通信:
其实就是多个线程在操作同一个资源。
但是操作的动作不同
*/
class Res
{
String name;
String sex;
}
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.name = "mike";
r.sex = "man";
}
else
{
r.name = "lili";
r.sex = "women";
}
x=(x+1)%2;
}
}
}
class Output implements Runnable
{
private Res r;
Output(Res r)
{
this.r = r;
}
public void run()
{
while(true)
{
System.out.println(r.name+"..."+r.sex);
}
}
}
class Test_12_1
{
public static void main(String[] args)
{
System.out.println("Hello Wolrd");
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
java线程间通信,布布扣,bubuko.com
java线程间通信