轮训线程,使每个线程输出5个数字
package org.example;
public class MulitThreadSerizialOut {
public static void main(String[] args) {
final PrintCls printCls = new PrintCls();
for(int i=0;i<3;i++){
new Thread(()->{
for(int j = 0;j<5;j++){
printCls.serizialOut(Thread.currentThread().getName());
}
}, i + "").start();
}
}
public static class PrintCls{
private int no = 1;
private int status = 0;
public synchronized void serizialOut(String threadName){
int threadIndex = Integer.parseInt(threadName);
while (threadIndex!=status){
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("this thread" + threadName+":");
for (int count = 0; count < 5; count++, no++) {
if (count > 0) {
System.out.print(",");
}
System.out.print(no);
}
System.out.println();
status = (status + 1) % 3;
this.notifyAll();
}
}
}