1. 程式人生 > >java基礎之多線程(3)閉鎖

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

ger 死循環 trac turn ktr adg throw boolean brush

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;
	}

	@Override
	public 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>() {
			@Override
			public 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;

	@Override
	public 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;

	@Override
	public 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)閉鎖