首页 » Java » java并发编程实战-CyclicBarrier和FutureTask使用

java并发编程实战-CyclicBarrier和FutureTask使用

原文 http://blog.csdn.net/tidu2chengfo/article/details/76218949

2017-07-27 22:20:05阅读(651)

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.FutureTask;
public class FutureTest {
	public static void main(String[] args) {
		new FutureTest().test();
	}
	
	public void test() {
		// 设置7个线程栅栏,为什么6个线程要设置7个栅栏,因为需要阻塞本线程等待6个线程的执行结果
        final CyclicBarrier cb = new CyclicBarrier(7);
        // 创建6个服务
        final Service[] assembles = new Service[6];
        final FutureTask[] futureTasks = new FutureTask[6];
        final Domain domain = new Domain();
        assembles[0] = new Service0();
        assembles[1] = new Service1();
        assembles[2] = new Service2();
        assembles[3] = new Service3();
        assembles[4] = new Service4();
        assembles[5] = new Service5();
        for(int i = 0; i < 6; i++) {
            final int fi = i;
            futureTasks[i] = new FutureTask<Integer>(new Callable<Integer>() {  
                @Override  
                public Integer call() throws Exception { 
                	try {
                		assembles[fi].assemble(domain);
                        System.out.println(fi + "assemble");
                	} finally {
                		cb.await();
                	}
                	return fi;
                }  
            });
            new Thread(futureTasks[i]).start(); 
        }
        try {
            cb.await();
        } catch (Exception e) {
            System.err.println(e);
        }
        for(int i = 0; i < 6; i++) {
        	try {
        		System.out.println(futureTasks[i].get());
			} catch (Exception e) {
				// 获取Service5抛出的异常
				System.err.println(e);
			} 
        }
        System.out.println(domain);
    }
	
	interface Service {
		void assemble(Domain domian);
	}
	class Domain{
		public String t0;
		public String t1;
		public String t2;
		public String t3;
		public String t4;
		@Override
		public String toString() {
			return t0 + "-" +t1 + "-" +t2 + "-" +t3 + "-" + t4;
		}
	}
	class Service0 implements Service {
		@Override
		public void assemble(Domain domian) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
			domian.t0 = "0";
		}
	}
	class Service1 implements Service {
		@Override
		public void assemble(Domain domian) {
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
			domian.t1 = "1";
		}
	}
	class Service2 implements Service {
		@Override
		public void assemble(Domain domian) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
			domian.t2 = "2";
		}
	}
	class Service3 implements Service {
		@Override
		public void assemble(Domain domian) {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
			domian.t3 = "3";
		}
	}
	class Service4 implements Service {
		@Override
		public void assemble(Domain domian) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
			domian.t4 = "4";
		}
	}
	class Service5 implements Service {
		@Override
		public void assemble(Domain domian) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
			throw new RuntimeException();
		}
	}
}

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹