我想用 T1-1、T2-2、T3-3、T1-4、T2-5、T3-6 等线程按顺序打印数字
public class NumberGame {
static int a=1;
public static void main(String args[]) throws InterruptedException
{
PrintSequenceRunnable C1=new PrintSequenceRunnable("T1",a);
PrintSequenceRunnable C2=new PrintSequenceRunnable("T2",a);
PrintSequenceRunnable C3=new PrintSequenceRunnable("T3",a);
Thread t1 = new Thread(C1);
Thread t2 = new Thread(C2);
Thread t3 = new Thread(C3);
t1.start();
t2.start();
t3.start();
}
}
public class PrintSequenceRunnable implements Runnable {
String tname;
int a;
PrintSequenceRunnable(String tname, int a )
{
this.tname = tname;
this.a = a;
}
@Override
public void run() {
synchronized (this) {
for(int i=0; i<10;i++)
{
System.out.println(tname+" "+a);
a++;
try {
this.wait(1000);
this.notify();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// TODO Auto-generated method stub
}
}
但我的输出就像
T1-1 T2-1 T3-1 T1-2 T3-2 T2-2 T3-3 T1-3 T2-3 T3-4 T1-4 T2-4 T3-5 T1-5 T2-5 T3-6 T1- 6 T2-6 T1-7 T2-7 T3-7 T2-8 T3-8 T1-8 T2-9 T3-9 T1-9 T2-10 T1-10 T3-10
谁能帮我。
慕田峪7331174
慕少森
相关分类