標籤: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)閉鎖