|
1 | 1 | package com.leetcode; |
2 | 2 |
|
3 | | -import java.util.concurrent.*; |
4 | | - |
5 | 3 | public class ThreadTest { |
6 | | - /////////////////生产者消费者模型//////////////////////////////////////////////////////// |
7 | | - public static class Goods { |
8 | | - private String name; |
9 | | - Goods(String name) { |
10 | | - this.name = name; |
11 | | - } |
12 | | - } |
13 | | - public static class Producer implements Runnable{ |
14 | | - private BlockingQueue<Goods> queue; |
15 | | - Producer(BlockingQueue<Goods> queue) { |
16 | | - this.queue = queue; |
17 | | - } |
18 | | - @Override |
19 | | - public void run() { |
20 | | - while (true) { |
21 | | - Goods good = new Goods("包子"); |
22 | | - try { |
23 | | - Thread.sleep(1000); |
24 | | - queue.put(good); |
25 | | - System.out.println(Thread.currentThread().getName() + "生产完成"); |
26 | | - } catch(Exception e) { |
27 | | - e.printStackTrace(); |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - } |
32 | | - public static class Consumer implements Runnable { |
33 | | - private BlockingQueue<Goods> queue; |
34 | | - Consumer(BlockingQueue<Goods> queue) { |
35 | | - this.queue = queue; |
36 | | - } |
37 | | - @Override |
38 | | - public void run() { |
39 | | - while (true) { |
40 | | - try { |
41 | | - Thread.sleep(1000); |
42 | | - Goods good = queue.take(); |
43 | | - System.out.println(Thread.currentThread().getName() + "消费完成"); |
44 | | - } catch (Exception e) { |
45 | | - e.printStackTrace(); |
46 | | - } |
47 | | - } |
48 | | - } |
49 | | - } |
50 | | - /////////////////生产者消费者模型//////////////////////////////////////////////////////// |
51 | | - /////////////////多线程打印奇数和偶数///////////////////////////////////////////////////// |
52 | | - public static class Number { |
53 | | - int num; |
54 | | - Number(int num) { |
| 4 | + public static class Thread3Print implements Runnable{ |
| 5 | + private static Object lock = new Object(); |
| 6 | + private static int count = 0; |
| 7 | + private int num; |
| 8 | + public Thread3Print(int num) { |
55 | 9 | this.num = num; |
56 | 10 | } |
57 | | - } |
58 | | - public static class EvenPrinter implements Runnable { |
59 | | - Number number; |
60 | | - EvenPrinter(Number number) { |
61 | | - this.number = number; |
62 | | - } |
63 | 11 | @Override |
64 | 12 | public void run() { |
65 | | - while (number.num <= 100) { |
66 | | -// if (Thread.currentThread().isInterrupted()) |
67 | | -// return; |
68 | | - synchronized(number) { |
69 | | - try { |
70 | | - Thread.sleep(100); |
71 | | - } catch (InterruptedException e) { |
72 | | - e.printStackTrace(); |
73 | | - } |
74 | | - if (number.num % 2 == 0) { |
75 | | - System.out.println(Thread.currentThread().getName() + "打印:" + number.num); |
76 | | - number.num += 1; |
77 | | - number.notify(); |
78 | | - } |
79 | | - else { |
| 13 | + synchronized (lock) { |
| 14 | + while (count <= 100) { |
| 15 | + if (count % 3 == num) { |
| 16 | + System.out.println(Thread.currentThread().getName() + "打印" + count); |
| 17 | + count += 1; |
| 18 | + lock.notifyAll(); |
| 19 | + } else { |
80 | 20 | try { |
81 | | - number.wait(); |
82 | | - } catch (Exception e) { |
83 | | - e.printStackTrace(); |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - public static class OddPrinter implements Runnable { |
91 | | - Number number; |
92 | | - OddPrinter(Number number) { |
93 | | - this.number = number; |
94 | | - } |
95 | | - @Override |
96 | | - public void run() { |
97 | | - while (number.num <= 100) { |
98 | | -// if (Thread.currentThread().isInterrupted()) |
99 | | -// return; |
100 | | - synchronized(number) { |
101 | | - if (number.num % 2 != 0) { |
102 | | - try { |
103 | | - Thread.sleep(100); |
| 21 | + lock.wait(); |
104 | 22 | } catch (InterruptedException e) { |
105 | 23 | e.printStackTrace(); |
106 | 24 | } |
107 | | - System.out.println(Thread.currentThread().getName() + "打印:" + number.num); |
108 | | - number.num += 1; |
109 | | - number.notify(); |
110 | | - } |
111 | | - else { |
112 | | - try { |
113 | | - number.wait(); |
114 | | - } catch (Exception e) { |
115 | | - e.printStackTrace(); |
116 | | - } |
117 | 25 | } |
118 | 26 | } |
119 | 27 | } |
120 | 28 | } |
121 | 29 | } |
122 | | - public static void main(String[] args) { |
123 | | -// BlockingQueue<Goods> queue = new ArrayBlockingQueue<>(5); |
124 | | -// Producer producer = new Producer(queue); |
125 | | -// Consumer consumer = new Consumer(queue); |
126 | | -// for (int i = 0; i < 2; i++) { |
127 | | -// new Thread(producer, "线程"+i).start(); |
128 | | -// } |
129 | | -// for (int i = 0; i < 2; i++) { |
130 | | -// new Thread(consumer, "线程"+i).start(); |
131 | | -// } |
132 | | - |
133 | | -// Number number = new Number(0); |
134 | | -// EvenPrinter evenPrinter = new EvenPrinter(number); |
135 | | -// OddPrinter oddPrinter = new OddPrinter(number); |
136 | | -// Thread evenThread = new Thread(evenPrinter, "偶数打印线程"); |
137 | | -// Thread oddThread = new Thread(oddPrinter, "奇数打印线程"); |
138 | | -// evenThread.start(); |
139 | | -// oddThread.start(); |
140 | | - |
141 | | - |
142 | | - |
143 | | -// //创建一个可缓存线程池 |
144 | | -// ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); |
145 | | -// for (int i = 0; i < 10; i++) { |
146 | | -// try { |
147 | | -// // sleep可明显看到使用的是线程池里面以前的线程,没有创建新的线程 |
148 | | -// Thread.sleep(1000); |
149 | | -// } catch (InterruptedException e) { |
150 | | -// e.printStackTrace(); |
151 | | -// } |
152 | | -// cachedThreadPool.execute(new Runnable() { |
153 | | -// public void run() { |
154 | | -// // 打印正在执行的缓存线程信息 |
155 | | -// System.out.println(Thread.currentThread().getName() |
156 | | -// + "正在被执行"); |
157 | | -// try { |
158 | | -// Thread.sleep(1000); |
159 | | -// } catch (InterruptedException e) { |
160 | | -// e.printStackTrace(); |
161 | | -// } |
162 | | -// } |
163 | | -// }); |
164 | | -// } |
165 | | - |
166 | | -// //定长线程池 |
167 | | -// ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); |
168 | | -// for (int i = 0; i < 10; i++) { |
169 | | -// fixedThreadPool.execute(new Runnable() { |
170 | | -// public void run() { |
171 | | -// try { |
172 | | -// // 打印正在执行的缓存线程信息 |
173 | | -// System.out.println(Thread.currentThread().getName() |
174 | | -// + "正在被执行"); |
175 | | -// Thread.sleep(2000); |
176 | | -// } catch (InterruptedException e) { |
177 | | -// e.printStackTrace(); |
178 | | -// } |
179 | | -// } |
180 | | -// }); |
181 | | -// } |
182 | | - ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); |
183 | | - //延迟1秒执行 |
184 | | - /*scheduledThreadPool.schedule(new Runnable() { |
185 | | - public void run() { |
186 | | - System.out.println("延迟1秒执行"); |
187 | | - } |
188 | | - }, 1, TimeUnit.SECONDS);*/ |
189 | | - |
190 | | - |
191 | | - //延迟1秒后每3秒执行一次 |
192 | | - scheduledThreadPool.scheduleAtFixedRate(new Runnable() { |
193 | | - public void run() { |
194 | | - System.out.println("延迟1秒后每3秒执行一次"); |
195 | | - } |
196 | | - }, 1, 3, TimeUnit.SECONDS); |
197 | | - |
198 | | - |
199 | | - |
200 | 30 |
|
201 | | -// try { |
202 | | -// Thread.sleep(2000); |
203 | | -// } catch (InterruptedException e) { |
204 | | -// e.printStackTrace(); |
205 | | -// } |
206 | | -// evenThread.interrupt(); |
207 | | -// oddThread.interrupt(); |
| 31 | + public static void main(String[] args) throws InterruptedException { |
| 32 | + Thread thread0 = new Thread(new Thread3Print(0), "线程0"); |
| 33 | + Thread thread1 = new Thread(new Thread3Print(1), "线程1"); |
| 34 | + Thread thread2 = new Thread(new Thread3Print(2), "线程2"); |
| 35 | + thread0.start(); |
| 36 | + thread1.start(); |
| 37 | + thread2.start(); |
| 38 | + thread0.join(); |
| 39 | + thread1.join(); |
| 40 | + thread2.join(); |
208 | 41 |
|
209 | 42 | } |
210 | 43 | } |
0 commit comments