52、java.util.concurrent 练习

import java.util.concurrent.*;
public class ExecutorsTest 
{
	public static void main(String[] args) 
	{
		//ExecutorService threadPool = Executors.newFixedThreadPool(3);//固定大小线程池
		//ExecutorService threadPool = Executors.newCachedThreadPool();//根据需要创建线程
		ExecutorService threadPool = Executors.newSingleThreadExecutor();//创建单个线程,可以解决:线程死掉后可以重新启动线程的问题。实际上是新建一个线程,跟重新启动线程效果一样
		for(int i=0;i<10;i++)
		{
			final int task = i+1;
			threadPool.execute(new Runnable(){
				public void run()
				{
					for(int j=0;j<10;j++)
						System.out.println(Thread.currentThread().getName()+" loop of "+(j+1)+" task of "+task);
				}
			});
		}
		threadPool.shutdown();//关闭线程池
		System.out.println("all of 10 tasks have committed");
	}
}

 

import java.util.concurrent.*;
public class ExecutorsTest 
{
	public static void main(String[] args) 
	{
		ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(3);
		/*threadPool.schedule(new Runnable(){
			public void run()
			{
				System.out.println(Thread.currentThread().getName()+" bombing");
			}
		},
			2,
			TimeUnit.SECONDS);
		thradPool.shutdown();
		*/
		threadPool.scheduleAtFixedRate(new Runnable(){
			public void run()
			{
				System.out.println("bombing");
			}
		},
			2,
			1,
			TimeUnit.SECONDS);
	}
}

 

import java.util.concurrent.*;
public class ExecutorsTest 
{
	public static void main(String[] args) 
	{
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		Future<Integer> future = threadPool.submit(new Callable<Integer>(){
			public Integer call()throws Exception
			{
				System.out.println(Thread.currentThread().getName());
				Thread.sleep(2000);
				return 3;
			}
		});
		try{
			System.out.println("main thread is coming");
			Integer res = future.get();//get()方法会造成线程阻塞
			System.out.println(res);
		}catch(Exception e){}

		threadPool.shutdown();
	}
}

 

import java.util.concurrent.*;
import java.util.*;
public class ExecutorsTest 
{
	public static void main(String[] args) 
	{
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		Collection<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
		Callable<Integer> task1 = new Callable<Integer>(){
			public Integer call()throws Exception
			{
				System.out.println(Thread.currentThread().getName()+" task 1");
				Thread.sleep(2000);
				return 111111;
			}
		};
		Callable<Integer> task2 = new Callable<Integer>(){
			public Integer call()throws Exception
			{
				System.out.println(Thread.currentThread().getName()+" task 2");
				Thread.sleep(2000);
				return 222222;
			}
		};
		Callable<Integer> task3 = new Callable<Integer>(){
			public Integer call()throws Exception
			{
				System.out.println(Thread.currentThread().getName()+" task 3");
				Thread.sleep(2000);
				return 333333;
			}
		};
		tasks.add(task1);
		tasks.add(task2);
		tasks.add(task3);
		System.out.println(tasks);
		try{
			List<Future<Integer>> retval = threadPool.invokeAll(tasks);//同时执行多个任务
			for(int i=0;i<retval.size();i++)
			{
				Integer res = retval.get(i).get();//get()方法会造成线程阻塞
				System.out.println(res);
			}
		}catch(Exception e){}
		threadPool.shutdown();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值