标签: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; } @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; } }
标签:for throw log static lis nts ack while catch
原文地址:http://www.cnblogs.com/gg128/p/7619796.html