
本文共 8645 字,大约阅读时间需要 28 分钟。
Executor������������
���Java 5���������������������������������������������������������������������������API���Executor������������Java 5���������������������������������������������������������java.util.concurrent ���������������������������������������������������������������������������������������������������������������������Java 5���������������Executor������������������������Thread���start������������������������������������������������������������������������������������������������������������������������������������this���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Executor������������������
Executor������������
Executor���������������������������������������������Executor���Executors���ExecutorService���CompletionService���Future���Callable������
Executor������
Executor������������������������������execute���Runnable command���������������������������Runable���������������������������������������������������������������Runnable���������������
ExecutorService������
ExecutorService���������������Executor���������������������������������������������������������������������ExecutorService��������������������������������������������������������������������������������������������������� Future ������������������������ExecutorService���shutdown������������������������������ ExecutorService���������������������������������ExecutorService������������������������������������������������������������������������(������������������������������������������������������������������������������������������������������)������������������������������������������������������������ExecutorService���
ExecutorService���������������
ExecutorService������������������������������������������������������������������������������������������������������������shutdown���������������������������������������������������������ExecutorService���������������������������������������������������������������������������������������������������������������������������������������������������������������shutdown���������������ExecutorService������������������������������������������������������������������������������������������������������������������������������������������������
Executors������������
Executors������������������������������������������������������������������������������������ExecutorService������������������
- public static ExecutorService newFixedThreadPool(int nThreads)������������������������������������������
- public static ExecutorService newCachedThreadPool()���������������������������������������
- public static ExecutorService newSingleThreadExecutor()������������������������������Executor���
- public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)���������������������������������������������������������������������
FixedThreadPool���CachedThreadPool���������
- newFixedThreadPool���������������������������������������������������������������������������������������������������������������������������������������
- newCachedThreadPool������������������������������������������������������������������������������60���������������������������
newSingleThreadExecutor
SingleThreadExecutor��������������������������������������������������������������������������������������������������������������������������������������������� executor������������������������������������������������������������������������������
Executor������Runnable������
������Executors��������������������������������������� ExecutorService ���������������������������������execute���Runnable command������������������������Runnable���������������execute������������������������������������������������������������������������
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class TestCachedThreadPool { public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { executorService.execute(new TestRunnable()); System.out.println("************* a" + i + " *************"); } executorService.shutdown(); }}class TestRunnable implements Runnable { public void run() { System.out.println(Thread.currentThread().getName() + "���������������������"); }}
������������������������submit���execute���������������������������������������������������������������������������������������������������������������������������������������������������������������
Executor������Callable������
���Java 5������������������������������������Runnable������������������������Callable���������������������������������ExecutorService������������Callable���������������������������������submit(Callable task) ������������������������������ Future���
Callable���������������Runnable������������������������������������������������������������������������������������Callable���������������Runnable������������������������Executor������Callable������������������
import java.util.ArrayList;import java.util.List;import java.util.concurrent.Future;import java.util.concurrent.FutureTask;public class CallableDemo { public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); List> resultList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Future future = executorService.submit(new TaskWithResult(i)); resultList.add(future); } for (Future fs : resultList) { try { while (!fs.isDone()) { System.out.println(fs.get()); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } finally { executorService.shutdown(); } } }}class TaskWithResult implements Callable { private int id; public TaskWithResult(int id) { this.id = id; } public String call() throws Exception { System.out.println("call()������������������������������" + Thread.currentThread().getName()); return "call()���������������������������������������������������" + id + " " + Thread.currentThread().getName(); }}
������������������
������ThreadPoolExecutor���������������������������������������
import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class ThreadPoolTest { public static void main(String[] args) { BlockingQueuebqueue = new ArrayBlockingQueue<>(20); ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 5, 50, TimeUnit.MILLISECONDS, bqueue); Runnable t1 = new MyThread(); Runnable t2 = new MyThread(); Runnable t3 = new MyThread(); Runnable t4 = new MyThread(); Runnable t5 = new MyThread(); Runnable t6 = new MyThread(); Runnable t7 = new MyThread(); pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); pool.execute(t6); pool.execute(t7); pool.shutdown(); }}class MyThread implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "���������������������"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } }}
ThreadPoolExecutor���������������
ThreadPoolExecutor������������������������������
- corePoolSize���������������������������������������������������������������
- maximumPoolSize������������������������������������
- keepAliveTime������������������������������������������������������������
- unit���������������������������
- workQueue������������������������������������������������������execute���������������Runnable���������
������
���������������������������������Executor������������������������������������������������������������������������������������������������������������������������������������executor������������FixedThreadPool���CachedThreadPool���ScheduledThreadPool���������������������������������������������������������������������������������������������������������������������������������ellie���������������������������
发表评论
最新留言
关于作者
