欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

exchanger关于互换情报的例子

更新时间: 2014-01-05 03:03:34 责任编辑: Author_N1

 

Exchanger关于交换情报的例子
package com.test;

import java.util.Random;
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExchangerTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		ExecutorService pool = Executors.newCachedThreadPool();
		final Exchanger changer = new Exchanger();
		pool.execute(new Runnable()
		{
			public void run()
			{
				String data1 = "情报1";
				System.out.println("线程:" + Thread.currentThread().getName()
						+ "准备把:" + data1+"交换出去");
				try
				{
					Thread.sleep(new Random().nextInt(10000));
					String data2 = (String)changer.exchange(data1);
					System.out.println("线程:" + Thread.currentThread().getName()
							+ "获得了:" + data2);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
		
		pool.execute(new Runnable()
		{
			public void run()
			{
				String data1 = "情报2";
				System.out.println("线程:" + Thread.currentThread().getName()
						+ "准备把:" + data1+"交换出去");
				try
				{
					Thread.sleep(new Random().nextInt(10000));
					String data2 = (String)changer.exchange(data1);
					System.out.println("线程:" + Thread.currentThread().getName()
							+ "获得了:" + data2);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
		
		pool.shutdown();
	}

}

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 03:03:34
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/17809/
本文WWW.DOC100.NET DOC100.NET版权所有。