java基础之多线程闭锁

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java基础之多线程闭锁相关的知识,希望对你有一定的参考价值。

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基础之多线程闭锁的主要内容,如果未能解决你的问题,请参考以下文章

java基础之多线程

Java基础之多线程

java基础之多线程二:多线程实现方式

java基础之多线程知识懂多少

java基础之多线程总结一(创建状态synchronized和volatile)

java基础之多线程总结一(创建状态synchronized和volatile)