三个线程轮流打印1-75,一个线程一次打印5个数

创建一个线程对应的类型,三个线程分开处理。

分别调用三个不同的线程,循环打印5个数字,打印完以后唤醒其他休眠的线程,然后自己休眠。

 1 package Thread;
 2 /**
 3  * 三个线程轮流打印1-75,一个线程一次打印5个数
 4  * @author Administrator
 5  *
 6  */
 7 
 8 public class ThreadDemo {
 9     //线程对应的类型,三个线程分开处理
10     static int type = 1;
11     static int number = 1;
12     public static void main(String[] args) {
13         new Thread("第一个线程:") { // 创建一个线程,命名为第一个线程
14             public void run() {
15                 try {
16                     synchronized (ThreadDemo.class) {
17                         while (number <= 75) {
18                             //当type的值为1的时候,由第一个线程来打印
19                             if (type == 1) {
20                                 for (int i = 0; i < 5; i++) {
21                                     System.out.println(Thread.currentThread().getName() + ":" + number++);
22                                 }
23                                 //修改类型值
24                                 type = 2;
25                                 //唤醒其他进入休眠的线程
26                                 ThreadDemo.class.notifyAll();
27                                 //当前线程执行完进入休眠状态
28                                 ThreadDemo.class.wait();
29                             } else {
30                                 ThreadDemo.class.wait();
31                             }
32                         }
33                     }
34                 } catch (InterruptedException e) {
35                     e.printStackTrace();
36                 }
37             };
38         }.start();
39         new Thread("第二个线程:") { // 创建二个线程,命名为第一个线程
40             public void run() {
41                 try {
42                     synchronized (ThreadDemo.class) {
43                         while (number <= 75) {
44                             if (type == 2) {
45                                 for (int i = 0; i < 5; i++) {
46                                     System.out.println(Thread.currentThread().getName() + ":" + number++);
47                                 }
48                                 type = 3;
49                                 ThreadDemo.class.notifyAll();
50                                 ThreadDemo.class.wait();
51                             } else {
52                                 ThreadDemo.class.wait();
53                             }
54                         }
55                     }
56                 } catch (InterruptedException e) {
57                     e.printStackTrace();
58                 }
59             };
60         }.start();
61         new Thread("第三个线程:") { // 创建三个线程,命名为第一个线程
62             public void run() {
63                 try {
64                     synchronized (ThreadDemo.class) {
65                         while (number <= 75) {
66                             if (type == 3) {
67                                 for (int i = 0; i < 5; i++) {
68                                     System.out.println(Thread.currentThread().getName() + ":" + number++);
69                                 }
70                                 type = 1;
71                                 ThreadDemo.class.notifyAll();
72                                 if (number < 76)
73                                     ;
74                                 ThreadDemo.class.wait();
75                             } else {
76                                 ThreadDemo.class.wait();
77                             }
78                         }
79                     }
80                 } catch (InterruptedException e) {
81                     e.printStackTrace();
82                 }
83             };
84         }.start();
85     }
86 }

猜你喜欢

转载自www.cnblogs.com/panghuang/p/9279565.html