File tree Expand file tree Collapse file tree
03concurrency/0301/src/main/java
out/production/JavaCourseCodes Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Manifest-Version : 1.0
2+ Main-Class :
3+
Original file line number Diff line number Diff line change @@ -13,16 +13,16 @@ public Integer call() throws Exception {
1313 }
1414 });
1515 new Thread (task ).start ();
16- //第二种方方式
17- // ExecutorService executor = Executors.newSingleThreadExecutor();
18- // FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
19- // @Override
20- // public Integer call() throws Exception {
21- // return new Random().nextInt();
22- // }
23- // } );
24- // executor.submit(task);
25-
16+ //第二种方方式// ExecutorService executor = Executors.newSingleThreadExecutor();
17+ //// FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
18+ //// @Override
19+ //// public Integer call() throws Exception {
20+ //// return new Random().nextInt();
21+ //// }
22+ //// });
23+ //// executor.submit(task );
24+ //
25+
2626 try {
2727 System .out .println ("result: " + task .get ());
2828 } catch (InterruptedException e ) {
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 ;
2+
3+ /**
4+ * 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池,
5+ * 异步运行一个方法,拿到这个方法的返回值后,退出主线程?
6+ * 写出你的方法,越多越好,提交到github。
7+ *
8+ * 一个简单的代码参考:
9+ */
10+ public class Homework03 {
11+
12+ public static void main (String [] args ) {
13+
14+ long start =System .currentTimeMillis ();
15+ // 在这里创建一个线程或线程池,
16+ // 异步执行 下面方法
17+
18+ int result = sum (); //这是得到的返回值
19+
20+ // 确保 拿到result 并输出
21+ System .out .println ("异步计算结果为:" +result );
22+
23+ System .out .println ("使用时间:" + (System .currentTimeMillis ()-start ) + " ms" );
24+
25+ // 然后退出main线程
26+ }
27+
28+ private static int sum () {
29+ return fibo (36 );
30+ }
31+
32+ private static int fibo (int a ) {
33+ if ( a < 2 )
34+ return 1 ;
35+ return fibo (a -1 ) + fibo (a -2 );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byFuture ;
2+
3+
4+
5+ import java .util .concurrent .Callable ;
6+ import java .util .concurrent .ExecutionException ;
7+ import java .util .concurrent .FutureTask ;
8+
9+ /**
10+ * @author tangtian
11+ * @version 1.0
12+ * @className FutureExample
13+ * @description
14+ * @date 2020/11/9 8:48 AM
15+ **/
16+ public class FutureTaskExample {
17+ public static void main (String [] args ) throws ExecutionException , InterruptedException {
18+ FutureTask <Integer > task = new FutureTask <Integer >(new Callable <Integer >() {
19+ @ Override
20+ public Integer call () throws Exception {
21+ return sum ();
22+ }
23+ });
24+ new Thread (task ).start ();
25+ System .out .println (task .get ());
26+ }
27+ private static int sum (){
28+ return fibo (39 );
29+ }
30+ private static int fibo (int a ){
31+ if (a < 2 ){
32+ return 1 ;
33+ }
34+ return fibo (a -1 ) + fibo (a - 2 );
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byFuture .byThreadpool ;
2+
3+
4+
5+ import java .util .concurrent .Callable ;
6+ import java .util .concurrent .ExecutionException ;
7+ import java .util .concurrent .ExecutorService ;
8+ import java .util .concurrent .Executors ;
9+ import java .util .concurrent .Future ;
10+
11+ /**
12+ * @author tangtian
13+ * @version 1.0
14+ * @className FutureExample
15+ * @description
16+ * @date 2020/11/9 8:48 AM
17+ **/
18+ public class FutureTaskExample {
19+ public static void main (String [] args ) throws ExecutionException , InterruptedException {
20+ ExecutorService executor = Executors .newCachedThreadPool ();
21+ Future <Integer > result = executor .submit (new Callable <Integer >() {
22+ public Integer call () throws Exception {
23+ return sum ();
24+ }
25+ });
26+ executor .shutdown ();
27+ try {
28+ System .out .println ("result:" + result .get ());
29+ } catch (InterruptedException e ) {
30+ e .printStackTrace ();
31+ } catch (ExecutionException e ) {
32+ e .printStackTrace ();
33+ }
34+ }
35+ private static int sum (){
36+ return fibo (39 );
37+ }
38+ private static int fibo (int a ){
39+ if (a < 2 ){
40+ return 1 ;
41+ }
42+ return fibo (a -1 ) + fibo (a - 2 );
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byconcurrentTool ;
2+
3+
4+ import java .util .concurrent .CountDownLatch ;
5+
6+ /**
7+ * @author tangtian
8+ * @version 1.0
9+ * @className text1
10+ * @description countDownLatch
11+ * @date 2020/11/9 6:26 AM
12+ **/
13+ public class CountDownLatchExample {
14+ private final static int threadCount = 20 ;
15+ public static void main (String [] args ) throws InterruptedException {
16+ final CountDownLatch countDownLatch = new CountDownLatch (threadCount );
17+ long start =System .currentTimeMillis ();
18+ for (int i = 0 ; i < threadCount ; i ++){
19+ new Thread (new Runnable () {
20+ @ Override
21+ public void run () {
22+ System .out .println (sum ());//开始执行任务
23+ countDownLatch .countDown ();
24+ }
25+ }).start ();
26+ }
27+ countDownLatch .await ();
28+ System .out .println ("使用时间:" + (System .currentTimeMillis ()-start ) + " ms" );
29+ }
30+ private static int sum (){
31+ return fibo (39 );
32+ }
33+ private static int fibo (int a ){
34+ if (a < 2 ){
35+ return 1 ;
36+ }
37+ return fibo (a -1 ) + fibo (a - 2 );
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byconcurrentTool ;
2+
3+
4+ import java .util .concurrent .BrokenBarrierException ;
5+ import java .util .concurrent .CyclicBarrier ;
6+
7+ /**
8+ * @author tangtian
9+ * @version 1.0
10+ * @className text1
11+ * @description CyclicBarrier
12+ * @date 2020/11/9 6:26 AM
13+ **/
14+ public class CyclicBarrierExample {
15+ private static CyclicBarrier barrier =new CyclicBarrier (2 ,()-> System .out .println ("currentResult" ));
16+ public static void main (String [] args ) throws InterruptedException {
17+ for (int i = 0 ; i < 10 ; i ++){
18+ new Thread (new Runnable () {
19+ @ Override
20+ public void run () {
21+ try {
22+ //等待线程准备好
23+ System .out .println (Thread .currentThread ().getName () + ":ready" );
24+ //开始执行任务
25+ System .out .println (Thread .currentThread ().getName () + "--result:" + sum ());
26+ //等待处理结果
27+ barrier .await ();
28+ System .out .println ("continue" );
29+ } catch (BrokenBarrierException e ) {
30+ e .printStackTrace ();
31+ } catch (InterruptedException e ) {
32+ e .printStackTrace ();
33+ }
34+
35+ }
36+ }).start ();
37+ }
38+ }
39+ private static int sum () throws BrokenBarrierException , InterruptedException {
40+ return fibo (39 );
41+ }
42+ private static int fibo (int a ){
43+ if (a < 2 ){
44+ return 1 ;
45+ }
46+ return fibo (a -1 ) + fibo (a - 2 );
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byconcurrentTool ;
2+
3+
4+ import java .util .concurrent .Semaphore ;
5+
6+ /**
7+ * @author tangtian
8+ * @version 1.0
9+ * @className Semaphore
10+ * @description
11+ * @date 2020/11/9 8:27 AM
12+ **/
13+ public class SemaphoreExample {
14+ private final static int threadCount = 20 ;
15+
16+ public static void main (String [] args ) {
17+ final Semaphore semaphore = new Semaphore (10 );
18+ for (int i = 0 ; i < threadCount ; i ++){
19+ new Thread (() -> {
20+ try {
21+ semaphore .acquire ();//获取许可
22+ System .out .println (sum ());//开始执行任务
23+ semaphore .release ();//释放个许可
24+ } catch (InterruptedException e ) {
25+ e .printStackTrace ();
26+ }
27+ }).start ();
28+ }
29+
30+ }
31+ private static int sum (){
32+ return fibo (39 );
33+ }
34+ private static int fibo (int a ){
35+ if (a < 2 ){
36+ return 1 ;
37+ }
38+ return fibo (a -1 ) + fibo (a - 2 );
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byconcurrentTool .byThreadpool ;
2+
3+
4+ import java .util .concurrent .CountDownLatch ;
5+ import java .util .concurrent .ExecutorService ;
6+ import java .util .concurrent .Executors ;
7+
8+ /**
9+ * @author tangtian
10+ * @version 1.0
11+ * @className text1
12+ * @description countDownLatch
13+ * @date 2020/11/9 6:26 AM
14+ **/
15+ public class CountDownLatchExample {
16+ private final static int threadCount = 200 ;
17+
18+ public static void main (String [] args ) throws Exception {
19+
20+ ExecutorService exec = Executors .newCachedThreadPool ();
21+
22+ final CountDownLatch countDownLatch = new CountDownLatch (threadCount );
23+
24+ for (int i = 0 ; i < threadCount ; i ++) {
25+ final int threadNum = i ;
26+ exec .execute (() -> {
27+ try {
28+ sum ();
29+ } catch (Exception e ) {
30+ e .printStackTrace ();
31+ } finally {
32+ countDownLatch .countDown ();
33+ }
34+ });
35+ }
36+ countDownLatch .await ();
37+ System .out .println ("程序执行完毕" );
38+ exec .shutdown ();
39+ }
40+ private static int sum (){
41+ return fibo (39 );
42+ }
43+ private static int fibo (int a ){
44+ if (a < 2 ){
45+ return 1 ;
46+ }
47+ return fibo (a -1 ) + fibo (a - 2 );
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0303 .homework3 .byconcurrentTool .byThreadpool ;
2+
3+
4+ import java .util .concurrent .BrokenBarrierException ;
5+ import java .util .concurrent .CyclicBarrier ;
6+
7+ /**
8+ * @author tangtian
9+ * @version 1.0
10+ * @className text1
11+ * @description CyclicBarrier
12+ * @date 2020/11/9 6:26 AM
13+ **/
14+ public class CyclicBarrierExample {
15+ private static CyclicBarrier barrier =new CyclicBarrier (2 ,()-> System .out .println ("currentResult" ));
16+ public static void main (String [] args ) throws InterruptedException {
17+ for (int i = 0 ; i < 10 ; i ++){
18+ new Thread (new Runnable () {
19+ @ Override
20+ public void run () {
21+ try {
22+ //等待线程准备好
23+ System .out .println (Thread .currentThread ().getName () + ":ready" );
24+ //开始执行任务
25+ System .out .println (Thread .currentThread ().getName () + "--result:" + sum ());
26+ //等待处理结果
27+ barrier .await ();
28+ System .out .println ("continue" );
29+ } catch (BrokenBarrierException e ) {
30+ e .printStackTrace ();
31+ } catch (InterruptedException e ) {
32+ e .printStackTrace ();
33+ }
34+
35+ }
36+ }).start ();
37+ }
38+ }
39+ private static int sum () throws BrokenBarrierException , InterruptedException {
40+ return fibo (39 );
41+ }
42+ private static int fibo (int a ){
43+ if (a < 2 ){
44+ return 1 ;
45+ }
46+ return fibo (a -1 ) + fibo (a - 2 );
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments