Java multi-thread enables three threads to output ABC10 times, and multi-thread abc10
I recently learned multithreading and searched for it. The problem of full screen is similar to that of the title. So let's start with this. I tried it myself,
I ran the computer CPU to 100% for multiple times and finally wrote it out. The general idea is:
Declare a variable and mark the output of the thread of the three threads. Each time the output adds the variable to 1, the judgment method is the remainder of the variable to 3. If it is 1-A, 2-B, 3-C
1 public class ABC { 2 3 private static int mark = 0; 4 5 private static Object obj = new Object(); 6 7 public static void main(String[] args) throws Exception { 8 ABC abc = new ABC(); 9 new Thread(abc.new PrintA()).start();10 new Thread(abc.new PrintB()).start();11 new Thread(abc.new PrintC()).start();12 }13 14 class PrintA implements Runnable{15 16 @Override17 public void run() {18 for(int i = 0; i < 10; i++){19 synchronized (obj){20 while(mark %3 != 0){21 try {22 obj.wait();23 } catch (InterruptedException e) {24 e.printStackTrace();25 }26 }27 System.out.print("A");28 mark ++;29 obj.notifyAll();30 }31 }32 }33 }34 class PrintB implements Runnable{35 36 @Override37 public void run() {38 for(int i = 0; i < 10; i++){39 synchronized (obj){40 while(mark %3 != 1){41 try {42 obj.wait();43 } catch (InterruptedException e) {44 e.printStackTrace();45 }46 }47 System.out.print("B");48 mark ++;49 obj.notifyAll();50 }51 }52 }53 }54 class PrintC implements Runnable{55 56 @Override57 public void run() {58 for(int i = 0; i < 10; i++){59 synchronized (obj){60 while(mark %3 != 2){61 try {62 obj.wait();63 } catch (InterruptedException e) {64 e.printStackTrace();65 }66 }67 System.out.println("C");68 mark ++;69 obj.notifyAll();70 }71 }72 }73 }74 }
Java multithreading requires three threads, one starting from 0, one storing and counting, and one output.
Synchronize the target variables in the counting, storing, and outputting methods. You can.
That is to say, when three threads use data at the same time, they should be lined up one by one.
Write A program, three threads Output A, B, and C respectively, and output ABC 10 times in sequence.
Package T;
Public class Test {
Public static void main (String [] args ){
Thread t1 = new test1 ();
Thread t2 = new test2 ();
Thread t3 = new test3 ();
T1.start ();
T2.start ();
T3.start ();
}
}
Class test1 extends Thread {
@ Override
Public void run (){
Super. run ();
For (int I = 0; I <= 9; I ++ ){
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("");
}
}
}
Class test2 extends Thread {
@ Override
Public void run (){
Super. run ();
For (int I = 0; I <= 9; I ++ ){
Try {
Thread. sleep (105 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("B ");
}
}
}
Class test3 extends Thread {
@ Override
Public void run (){
Super. run ();
For (int I = 0; I <= 9; I ++ ){
Try {
Thread. sleep (110 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("C ");
}
}
}