java基礎之多線程(3)閉鎖

來源:互聯網
上載者:User

標籤:for   throw   log   static   lis   nts   ack   while   catch   

1.閉鎖方式1:利用CountDownLatch進行閉鎖

import java.util.concurrent.CountDownLatch;public class CloseLock3 {public static void main(String[] args) {long start = System.currentTimeMillis();CountDownLatch latch = new CountDownLatch(5);CountEven even = new CountEven(latch);for (int i = 1; i <= 5; i++) {new Thread(even).start();}try {latch.await();} catch (InterruptedException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println("偶數個數為:" + even.getNum());System.out.println("耗時為:" + (end - start));}}class CountEven implements Runnable {private int i = 100;private boolean flag = true;private int num;private CountDownLatch latch;public CountEven(CountDownLatch latch) {super();this.latch = latch;}@Overridepublic void run() {try {while (flag) {synchronized (this) {if (i >= 0) {if (i % 2 == 0) {System.out.println(Thread.currentThread().getName() + ":" + i + "是一個偶數");num++;}i--;} else {flag = false;}}}} finally {latch.countDown();}}public int getNum() {return num;}}

 2.閉鎖方式2:利用Callable的傳回值進行閉鎖

import java.util.concurrent.Callable;import java.util.concurrent.FutureTask;public class CloseLock {public static void main(String[] args) {long start = System.currentTimeMillis();CountEven even = new CountEven();FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {even.even();return null;}});new Thread(task, "線程1").start();try {task.get();//阻塞式方法} catch (Exception e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println("偶數個數為:" + even.getNum());System.out.println("耗時為:" + (end - start));}}class CountEven {private int i = 100;private boolean flag = true;private int num;public void even() {while (flag) {synchronized (this) {if (i >= 0) {if (i % 2 == 0) {System.out.println(Thread.currentThread().getName() + ":" + i + "是一個偶數");num++;}i--;} else {flag = false;}}}}public int getNum() {return num;}}

 3. 利用isalive進行閉鎖

public class CloseLock {public static void main(String[] args) {long start = System.currentTimeMillis();CountEven even = new CountEven();Thread thread = new Thread(even);thread.start();while (thread.isAlive()) {          //thread線程沒結束則一直死迴圈}long end = System.currentTimeMillis();System.out.println("偶數個數為:" + even.getNum());System.out.println("耗時為:" + (end - start));}}class CountEven implements Runnable {private int i = 100;private boolean flag = true;private int num;@Overridepublic void run() {while (flag) {synchronized (this) {if (i >= 0) {if (i % 2 == 0) {System.out.println(Thread.currentThread().getName() + ":" + i + "是一個偶數");num++;}i--;} else {flag = false;}}}}public int getNum() {return num;}}

 4.利用線程組進行閉鎖

public class CloseLock {public static void main(String[] args) {long start = System.currentTimeMillis();CountEven even = new CountEven();ThreadGroup group = new ThreadGroup("線程組1");Thread thread = new Thread(group,even);Thread thread1 = new Thread(group,even);thread.start();thread1.start();while (group.activeCount()!=0) {//activeCount()方法主要用於測試}long end = System.currentTimeMillis();System.out.println("偶數個數為:" + even.getNum());System.out.println("耗時為:" + (end - start));}}class CountEven implements Runnable {private int i = 100;private boolean flag = true;private int num;@Overridepublic void run() {while (flag) {synchronized (this) {if (i >= 0) {if (i % 2 == 0) {System.out.println(Thread.currentThread().getName() + ":" + i + "是一個偶數");num++;}i--;} else {flag = false;}}}}public int getNum() {return num;}}

  

  

java基礎之多線程(3)閉鎖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.