class Res
{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable
{
private Res r;
Input(Res r)
{
this.r=r;
}
public void run()
{
int y=0;
while(true) //如果我想控制打印次数是不是把这改写成 for(int x =0;x<100;x++)
{
if(y==0)
{
r.name="张三";
r.sex="男";
}
else
{
r.name="李四";
r.sex="女";
}
y=(y+1)%2;
}
}
}
class Ouput implements Runnable
{
private Res r; //不明白这个地方怎么引用的Input中的name 和 sex的? Ouput(Res r)
{
this.r=r;
}
public void run() //如果我建立一个 start();方法 那么多线程运行start();方法时会运行run方法还是start方法? {
while(true)
{
System.out.println(r.name+"!!!!!!!!!"+r.sex);
}
}
}
class ResDemo
{
public static void main(String[] args)
{
Res r = new Res();
Ouput Ou = new Ouput(r);
Input In = new Input(r);
Thread t1 = new Thread(In);
Thread t2 = new Thread(Ou);
t1.start();
t2.start();
}
}
class Res
{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable
{
private Res r;
Input(Res r)
{
this.r=r;
}
public void run()
{
int y=0;
class ResDemo
{
public static void main(String[] args)
{
Res r = new Res();
Ouput Ou = new Ouput(r);
Input In = new Input(r);
Thread t1 = new Thread(In);
Thread t2 = new Thread(Ou);
t1.start();
t2.start();
}
}