初学线程,请帮下忙!!
程序代码:
package 线程; class Reasouce1{ String name; String sex; } class Input1 implements Runnable{ Reasouce1 r; public Input1(Reasouce1 r) { this.r=r; } public void run() { int flag=0; while(true){ synchronized(r) { if(flag==0) { r.name="旺财"; r.sex="男"; } else { r.name="小强"; r.sex="女女女女"; } } flag=(flag+1)%2; } } } class Output1 implements Runnable{ Reasouce1 r; Output1(Reasouce1 r) { this.r=r; } public void run() { while(true){ synchronized(r) { System.out.println(r.name+".........."+r.sex); } } } } public class ThreadDemo3 { public static void main(String[] args) { Reasouce1 r=new Reasouce1(); Input1 in=new Input1(r); Output1 out=new Output1(r); Thread t2=new Thread(in); Thread t1=new Thread(out); t2.start(); t1.start(); } }这个代码怎么回事啊!!