Starting 3 threads, thread 1 printing 1 to 5, thread 2 printing 5 to 10, thread 3 printing 11 to 15, then thread 1 printing 16 to 20, and so on ...
Print until 30 public class Mainthread {private static int num;//current record number private static final int threadnum =3;//open Task thread count private static final int loopnum = number of 2;//threadnum thread loops private static final int singlethreadloopnum = 5;//single line
Number of running tasks static {num = 0; Public synchronized void Callbackmethod (String threadname) {for (int i = 1; I <= singlethreadloopnum;
i++) {System.out.println (threadname+ ":" + (++num));
}} public void Printnum () throws interruptedexception{for (int i = 1; I <=loopNum; i++) { for (int j = 1; J <= Threadnum; j + +) {worker runnable = new Worker (this);//this the current record Count num
Pass to the next thread, thread thread = new Thread (runnable);
Thread.setname ("thread" +j);
Thread.Start ();
Thread.Join ();
}
}
} public static void Main (string[] args) throws interruptedexception{mainthread pc = new Mainthread (); Pc.
Printnum ();
}} class Worker implements runnable{private Mainthread printcallback;
Public Worker (Mainthread printcallback) {this.printcallback = Printcallback;
public void Run () {Printcallback.callbackmethod (Thread.CurrentThread (). GetName ());
}
}