File tree Expand file tree Collapse file tree 5 files changed +154
-1
lines changed Expand file tree Collapse file tree 5 files changed +154
-1
lines changed Original file line number Diff line number Diff line change 1+ package com .duwei .multythread .util ;
2+
3+ import java .util .concurrent .CountDownLatch ;
4+
5+ /**
6+ * 使用CountDownLatch可以使其他任务执行完后再执行接下来的代码,相当于计数
7+ */
8+ public class CountDownLatchDemo {
9+ public static void main (String [] args ) {
10+ final CountDownLatch latch = new CountDownLatch (2 );
11+ new Thread () {
12+ public void run () {
13+ try {
14+ System .out .println ("子线程" + Thread .currentThread ().getName () + "正在执行" );
15+ Thread .sleep (3000 );
16+ System .out .println ("子线程" + Thread .currentThread ().getName () + "执行完毕" );
17+ latch .countDown ();
18+ } catch (InterruptedException e ) {
19+ e .printStackTrace ();
20+ }
21+ }
22+
23+ ;
24+ }.start ();
25+
26+ new Thread () {
27+ public void run () {
28+ try {
29+ System .out .println ("子线程" + Thread .currentThread ().getName () + "正在执行" );
30+ Thread .sleep (3000 );
31+ System .out .println ("子线程" + Thread .currentThread ().getName () + "执行完毕" );
32+ latch .countDown ();
33+ } catch (InterruptedException e ) {
34+ e .printStackTrace ();
35+ }
36+ }
37+
38+ ;
39+ }.start ();
40+
41+ try {
42+ System .out .println ("等待2个子线程执行完毕..." );
43+ latch .await ();
44+ System .out .println ("2个子线程已经执行完毕" );
45+ System .out .println ("继续执行主线程" );
46+ } catch (InterruptedException e ) {
47+ e .printStackTrace ();
48+ }
49+ }
50+
51+
52+ }
Original file line number Diff line number Diff line change 1+ package com .duwei .multythread .util ;
2+
3+ import java .util .concurrent .BrokenBarrierException ;
4+ import java .util .concurrent .CyclicBarrier ;
5+
6+ /**
7+ * 字面意思回环栅栏,通过它可以实现让一组线程等待至某个状态之后再全部同时执行。
8+ * 叫做回环是因为当所有等待线程都被释放以后,CyclicBarrier可以被重用。
9+ * 我们暂且把这个状态就叫做barrier,当调用await()方法之后,线程就处于barrier了。
10+ *
11+ * http://www.importnew.com/21889.html
12+ */
13+ public class CyclicBarrierDemo {
14+
15+ public static void main (String [] args ) {
16+ int N = 4 ;
17+ CyclicBarrier barrier = new CyclicBarrier (N );
18+ for (int i = 0 ; i < N ; i ++)
19+ new Writer (barrier ).start ();
20+ }
21+
22+ static class Writer extends Thread {
23+ private CyclicBarrier cyclicBarrier ;
24+
25+ public Writer (CyclicBarrier cyclicBarrier ) {
26+ this .cyclicBarrier = cyclicBarrier ;
27+ }
28+
29+ @ Override
30+ public void run () {
31+ System .out .println ("线程" + Thread .currentThread ().getName () + "正在写入数据..." );
32+ try {
33+ Thread .sleep (5000 ); //以睡眠来模拟写入数据操作
34+ System .out .println ("线程" + Thread .currentThread ().getName () + "写入数据完毕,等待其他线程写入完毕" );
35+ cyclicBarrier .await ();
36+ } catch (InterruptedException e ) {
37+ e .printStackTrace ();
38+ } catch (BrokenBarrierException e ) {
39+ e .printStackTrace ();
40+ }
41+ System .out .println ("所有线程写入完毕,继续处理其他任务..." );
42+ }
43+ }
44+
45+ }
Original file line number Diff line number Diff line change 1+ package com .duwei .multythread .util ;
2+
3+ import java .util .concurrent .Semaphore ;
4+
5+ /**
6+ * Semaphore翻译成字面意思为 信号量,Semaphore可以控同时访问的线程个数,
7+ * 通过 acquire() 获取一个许可,如果没有就等待,而 release() 释放一个许可。
8+ */
9+ public class SemaphoreDemo {
10+
11+ /**
12+ * 假若一个工厂有5台机器,但是有8个工人,一台机器同时只能被一个工人使用,只有使用完了,
13+ * 其他工人才能继续使用。那么我们就可以通过Semaphore来实现:
14+ *
15+ * @param args
16+ */
17+ public static void main (String [] args ) {
18+ int N = 8 ; //工人数
19+ Semaphore semaphore = new Semaphore (5 ); //机器数目
20+ for (int i = 0 ; i < N ; i ++)
21+ new Worker (i , semaphore ).start ();
22+ }
23+
24+ static class Worker extends Thread {
25+ private int num ;
26+ private Semaphore semaphore ;
27+
28+ public Worker (int num , Semaphore semaphore ) {
29+ this .num = num ;
30+ this .semaphore = semaphore ;
31+ }
32+
33+ @ Override
34+ public void run () {
35+ try {
36+ semaphore .acquire ();
37+ System .out .println ("工人" + this .num + "占用一个机器在生产..." );
38+ Thread .sleep (2000 );
39+ System .out .println ("工人" + this .num + "释放出机器" );
40+ semaphore .release ();
41+ } catch (InterruptedException e ) {
42+ e .printStackTrace ();
43+ }
44+ }
45+ }
46+ }
Original file line number Diff line number Diff line change 1+ package com .duweri .interview ;
2+
3+ public class ByteComput {
4+
5+ public static void main (String [] args ) {
6+
7+
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 11package com .duweri .interview .sort ;
22
3- public class BubbleSort extends BaseSortAlgorithms {
3+ public class BubbleSort extends BaseSortAlgorithms {
44 /*
55 * 冒泡排序
66 * 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
You can’t perform that action at this time.
0 commit comments