`

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();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics