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.8injava.exe "-javaagent:H:IntelliJ IDEA 2020.3.2libidea_rt.jar=53950:H:IntelliJ IDEA 2020.3.2in" -Dfile.encoding=UTF-8 -classpath H:JDK1.8jrelibcharsets.jar;H:JDK1.8jrelibdeploy.jar;H:JDK1.8jrelibextaccess-bridge-64.jar;H:JDK1.8jrelibextcldrdata.jar;H:JDK1.8jrelibextdnsns.jar;H:JDK1.8jrelibextjaccess.jar;H:JDK1.8jrelibextjfxrt.jar;H:JDK1.8jrelibextlocaledata.jar;H:JDK1.8jrelibext
ashorn.jar;H:JDK1.8jrelibextsunec.jar;H:JDK1.8jrelibextsunjce_provider.jar;H:JDK1.8jrelibextsunmscapi.jar;H:JDK1.8jrelibextsunpkcs11.jar;H:JDK1.8jrelibextzipfs.jar;H:JDK1.8jrelibjavaws.jar;H:JDK1.8jrelibjce.jar;H:JDK1.8jrelibjfr.jar;H:JDK1.8jrelibjfxswt.jar;H:JDK1.8jrelibjsse.jar;H:JDK1.8jrelibmanagement-agent.jar;H:JDK1.8jrelibplugin.jar;H:JDK1.8jrelib
esources.jar;H:JDK1.8jrelib
t.jar;H:A_WorkCodedemo argetclasses;H:A_WorkRepositoryorgspringframeworkcloudspring-cloud-starter3.0.0spring-cloud-starter-3.0.0.jar;H:A_WorkRepositoryorgspringframeworkootspring-boot-starter2.4.2spring-boot-starter-2.4.2.jar;H:A_WorkRepositoryorgspringframeworkootspring-boot-starter-logging2.4.2spring-boot-starter-logging-2.4.2.jar;H:A_WorkRepositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;H:A_WorkRepositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;H:A_WorkRepositoryorgapachelogginglog4jlog4j-to-slf4j2.13.3log4j-to-slf4j-2.13.3.jar;H:A_WorkRepositoryorgapachelogginglog4jlog4j-api2.13.3log4j-api-2.13.3.jar;H:A_WorkRepositoryorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;H:A_WorkRepositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;H:A_WorkRepositoryorgyamlsnakeyaml1.27snakeyaml-1.27.jar;H:A_WorkRepositoryorgspringframeworkcloudspring-cloud-context3.0.0spring-cloud-context-3.0.0.jar;H:A_WorkRepositoryorgspringframeworksecurityspring-security-crypto5.4.2spring-security-crypto-5.4.2.jar;H:A_WorkRepositoryorgspringframeworkcloudspring-cloud-commons3.0.0spring-cloud-commons-3.0.0.jar;H:A_WorkRepositoryorgspringframeworksecurityspring-security-rsa1.0.9.RELEASEspring-security-rsa-1.0.9.RELEASE.jar;H:A_WorkRepositoryorgouncycastlecpkix-jdk15on1.64cpkix-jdk15on-1.64.jar;H:A_WorkRepositoryorgouncycastlecprov-jdk15on1.64cprov-jdk15on-1.64.jar;H:A_WorkRepositoryorgspringframeworkootspring-boot-devtools2.4.2spring-boot-devtools-2.4.2.jar;H:A_WorkRepositoryorgspringframeworkootspring-boot2.4.2spring-boot-2.4.2.jar;H:A_WorkRepositoryorgspringframeworkspring-context5.3.3spring-context-5.3.3.jar;H:A_WorkRepositoryorgspringframeworkspring-aop5.3.3spring-aop-5.3.3.jar;H:A_WorkRepositoryorgspringframeworkspring-beans5.3.3spring-beans-5.3.3.jar;H:A_WorkRepositoryorgspringframeworkspring-expression5.3.3spring-expression-5.3.3.jar;H:A_WorkRepositoryorgspringframeworkootspring-boot-autoconfigure2.4.2spring-boot-autoconfigure-2.4.2.jar;H:A_WorkRepositoryorgprojectlomboklombok1.18.16lombok-1.18.16.jar;H:A_WorkRepositoryorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;H:A_WorkRepositoryorgspringframeworkspring-core5.3.3spring-core-5.3.3.jar;H:A_WorkRepositoryorgspringframeworkspring-jcl5.3.3spring-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