其他分享
首页 > 其他分享> > 三个线程交替打印十次ABC

三个线程交替打印十次ABC

作者:互联网

 1 package com.example.demo.thread;
 2 
 3 /**
 4  * @author wangdongjiang
 5  */
 6 public class Work {
 7     private volatile static String flag = "A";
 8     private static int countA = 0;
 9     private static int countB = 0;
10     private static int countC = 0;
11 
12     public void methodA() {
13         while (true) {
14             //如果打印次数小于10就打印
15             if (countA < 10 && flag.equals("A")) {
16                 System.out.println("A");
17                 flag = "B";
18                 countA = countA + 1;
19             }
20         }
21     }
22 
23     public void methodB() {
24         while (true) {
25             //如果打印次数小于10就打印
26             if (countB < 10 && flag.equals("B")) {
27                 System.out.println("B");
28                 flag = "C";
29                 countB = countB + 1;
30             }
31         }
32     }
33 
34 
35     public void methodC() {
36         while (true) {
37             //如果打印次数小于10就打印
38             if (countC < 10 && flag.equals("C")) {
39                 System.out.println("C");
40                 flag = "A";
41                 countC = countC + 1;
42             }
43         }
44     }
45 }
 1 package com.example.demo.thread;
 2 
 3 public class MyA implements Runnable {
 4 
 5     private Work work;
 6 
 7     public MyA(Work work) {
 8         this.work = work;
 9     }
10 
11     @Override
12     public void run() {
13         work.methodA();
14     }
15 }
 1 package com.example.demo.thread;
 2 
 3 public class MyB implements Runnable {
 4 
 5     private Work work;
 6 
 7     public MyB(Work work) {
 8         this.work = work;
 9     }
10 
11     @Override
12     public void run() {
13         work.methodB();
14     }
15 }
 1 package com.example.demo.thread;
 2 
 3 public class MyC implements Runnable {
 4 
 5     private Work work;
 6 
 7     public MyC(Work work) {
 8         this.work = work;
 9     }
10 
11     @Override
12     public void run() {
13         work.methodC();
14     }
15 }
 1 package com.example.demo.thread;
 2 
 3 /**
 4  * @author wangdongjiang
 5  */
 6 public class Hello {
 7 
 8     public static void main(String[] args) throws InterruptedException {
 9         final Work work = new Work();
10         final MyA myA = new MyA(work);
11         final MyB myB = new MyB(work);
12         final MyC myC = new MyC(work);
13 
14         final Thread thread1 = new Thread(myA);
15         final Thread thread2 = new Thread(myB);
16         final Thread thread3 = new Thread(myC);
17 
18         thread1.start();
19         thread2.start();
20         thread3.start();
21 
22         Thread.sleep(100000000L);
23 
24     }
25 
26 
27 }

打印结果:

H:\JDK1.8\bin\java.exe "-javaagent:H:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=53950:H:\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath H:\JDK1.8\jre\lib\charsets.jar;H:\JDK1.8\jre\lib\deploy.jar;H:\JDK1.8\jre\lib\ext\access-bridge-64.jar;H:\JDK1.8\jre\lib\ext\cldrdata.jar;H:\JDK1.8\jre\lib\ext\dnsns.jar;H:\JDK1.8\jre\lib\ext\jaccess.jar;H:\JDK1.8\jre\lib\ext\jfxrt.jar;H:\JDK1.8\jre\lib\ext\localedata.jar;H:\JDK1.8\jre\lib\ext\nashorn.jar;H:\JDK1.8\jre\lib\ext\sunec.jar;H:\JDK1.8\jre\lib\ext\sunjce_provider.jar;H:\JDK1.8\jre\lib\ext\sunmscapi.jar;H:\JDK1.8\jre\lib\ext\sunpkcs11.jar;H:\JDK1.8\jre\lib\ext\zipfs.jar;H:\JDK1.8\jre\lib\javaws.jar;H:\JDK1.8\jre\lib\jce.jar;H:\JDK1.8\jre\lib\jfr.jar;H:\JDK1.8\jre\lib\jfxswt.jar;H:\JDK1.8\jre\lib\jsse.jar;H:\JDK1.8\jre\lib\management-agent.jar;H:\JDK1.8\jre\lib\plugin.jar;H:\JDK1.8\jre\lib\resources.jar;H:\JDK1.8\jre\lib\rt.jar;H:\A_Work\Code\demo\target\classes;H:\A_Work\Repository\org\springframework\cloud\spring-cloud-starter\3.0.0\spring-cloud-starter-3.0.0.jar;H:\A_Work\Repository\org\springframework\boot\spring-boot-starter\2.4.2\spring-boot-starter-2.4.2.jar;H:\A_Work\Repository\org\springframework\boot\spring-boot-starter-logging\2.4.2\spring-boot-starter-logging-2.4.2.jar;H:\A_Work\Repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;H:\A_Work\Repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;H:\A_Work\Repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;H:\A_Work\Repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;H:\A_Work\Repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;H:\A_Work\Repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;H:\A_Work\Repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;H:\A_Work\Repository\org\springframework\cloud\spring-cloud-context\3.0.0\spring-cloud-context-3.0.0.jar;H:\A_Work\Repository\org\springframework\security\spring-security-crypto\5.4.2\spring-security-crypto-5.4.2.jar;H:\A_Work\Repository\org\springframework\cloud\spring-cloud-commons\3.0.0\spring-cloud-commons-3.0.0.jar;H:\A_Work\Repository\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;H:\A_Work\Repository\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;H:\A_Work\Repository\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;H:\A_Work\Repository\org\springframework\boot\spring-boot-devtools\2.4.2\spring-boot-devtools-2.4.2.jar;H:\A_Work\Repository\org\springframework\boot\spring-boot\2.4.2\spring-boot-2.4.2.jar;H:\A_Work\Repository\org\springframework\spring-context\5.3.3\spring-context-5.3.3.jar;H:\A_Work\Repository\org\springframework\spring-aop\5.3.3\spring-aop-5.3.3.jar;H:\A_Work\Repository\org\springframework\spring-beans\5.3.3\spring-beans-5.3.3.jar;H:\A_Work\Repository\org\springframework\spring-expression\5.3.3\spring-expression-5.3.3.jar;H:\A_Work\Repository\org\springframework\boot\spring-boot-autoconfigure\2.4.2\spring-boot-autoconfigure-2.4.2.jar;H:\A_Work\Repository\org\projectlombok\lombok\1.18.16\lombok-1.18.16.jar;H:\A_Work\Repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;H:\A_Work\Repository\org\springframework\spring-core\5.3.3\spring-core-5.3.3.jar;H:\A_Work\Repository\org\springframework\spring-jcl\5.3.3\spring-jcl-5.3.3.jar com.example.demo.thread.Hello
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C

标签:十次,ABC,lib,Repository,spring,Work,jar,线程,org
来源: https://www.cnblogs.com/dongjiang/p/14402528.html