多线程学习二

 1 package com.service.base;
 2 
 3 public class Actor extends Thread {
 4 
 5     @Override
 6     public void run() {
 7 
 8         System.out.println(getName()+"是一个演员!");
 9         
10         boolean isBoolCheck = true;
11         int counter = 0 ;
12          while(isBoolCheck){
13              
14             System.out.println(getName()+"演出表演!"+ ++counter);
15 
16             if(counter%10 ==0){
17                 
18             try {
19                 Thread.sleep(1000);
20             } catch (InterruptedException e) {
21                 e.printStackTrace();
22             }
23             
24             if(counter==100){
25                 
26                 isBoolCheck = false;
27             }
28                 
29             }
30          }
31         System.out.println(getName()+"表演结束!");
32     }
33     
34     
35     public static void main(String[] args) {
36         
37         Thread actor  = new Actor();
38         actor.setName("男演员");
39         actor.start();
40         
41         Thread actress = new Thread(new Actress(),"女演员");
42         actress.start();
43     }
44 }
45 
46 class Actress implements Runnable{
47 
48     @Override
49     public void run() {
50 
51 
52 
53         System.out.println(Thread.currentThread().getName()+"是一个演员!");
54         
55         boolean isBoolCheck = true;
56         int counter = 0 ;
57          while(isBoolCheck){
58              
59             System.out.println(Thread.currentThread().getName()+"演出表演!"+ ++counter);
60 
61             if(counter%10 ==0){
62                 
63             try {
64                 Thread.sleep(1000);
65             } catch (InterruptedException e) {
66                 e.printStackTrace();
67             }
68             
69             if(counter==100){
70                 
71                 isBoolCheck = false;
72             }
73                 
74             }
75          }
76         System.out.println(Thread.currentThread().getName()+"表演结束!");
77     }
78     
79 }
 1 package com.service.base;
 2 
 3 public class ArmyRunnable implements Runnable {
 4 
 5     volatile boolean isBoolCheck = true ;
 6     
 7     @Override
 8     public void run() {
 9 
10         while(isBoolCheck){
11             
12             for (int i = 0; i < 5; i++) {
13                     System.out.println(Thread.currentThread().getName()+"进攻对方【"+ i +"】");
14 //                Thread.yield();
15             }
16         }
17         
18         System.out.println(Thread.currentThread().getName()+"结束战斗!");
19     }
20 
21 }
 1 package com.service.base;
 2 
 3 public class KeyPersonThread extends Thread {
 4     
 5     @Override
 6     public void run() {
 7         
 8         System.out.println(Thread.currentThread().getName()+"开始战斗!");
 9         
10         for (int i = 0; i < 10; i++) {
11             System.out.println(Thread.currentThread().getName()+"左突右杀,大战隋军。");
12         }
13         
14         System.out.println(Thread.currentThread().getName()+"结束战斗!");
15         
16         
17     }
18 
19 }
 1 package com.service.base;
 2 
 3 public class Stage extends Thread {
 4 
 5     @Override
 6     public void run() {
 7 
 8      System.out.println("欢迎观看隋唐演义!");
 9      try {
10          Thread.sleep(2000);
11      } catch (InterruptedException e1) {
12          e1.printStackTrace();
13      }
14     System.out.println("大幕徐徐拉开!"); 
15     try {
16         Thread.sleep(2000);
17     } catch (InterruptedException e1) {
18         e1.printStackTrace();
19     }
20     
21      ArmyRunnable armySUI = new ArmyRunnable(); 
22      ArmyRunnable armyTang = new ArmyRunnable(); 
23      
24      Thread sui = new Thread(armySUI,"隋军");
25      Thread tang = new Thread(armyTang,"唐军");
26      
27      sui.start();
28      tang.start();
29      
30      
31      try {
32         Thread.sleep(50);
33     } catch (InterruptedException e) {
34         e.printStackTrace();
35     }
36      
37      System.out.println("正当双方激战正酣,半路杀出个程咬金!");
38     
39      Thread key = new KeyPersonThread();
40      key.setName("程咬金");
41      
42      System.out.println("程咬金的理想是结束战斗,是百姓安居和业。");
43      
44      armySUI.isBoolCheck = false;
45      armyTang.isBoolCheck = false;
46      
47      try {
48         Thread.sleep(2000);
49     } catch (InterruptedException e1) {
50         e1.printStackTrace();
51     }
52      
53      key.start();
54      
55      
56      try {
57          key.join();
58     } catch (InterruptedException e) {
59         e.printStackTrace();
60     }
61      
62      System.out.println("战争结束,程先生实现个人理想,百姓安居乐业!");
63      try {
64             Thread.sleep(2000);
65         } catch (InterruptedException e1) {
66             e1.printStackTrace();
67         }
68      System.out.println("谢谢收看隋唐演义,再见!");
69     }
70     
71     public static void main(String[] args) {
72         
73         new Stage().start();
74         
75     }
76 }
抱怨没有用,只能靠自己
原文地址:https://www.cnblogs.com/mybatis/p/5896186.html