??????????????????????????е???????????????????????????????к???С?
????class SingleThreadWebServer {
????public static void main(String[] args) throws Exception {
????ServerSocket socket = new ServerSocket(80);
????while(true) {
????Socket conn = socket.accept();
????handleRequest(conn);
????}
????}
????}
????class ThreadPerTaskWebServer {
????public static void main(String[] args) throws Exception {
????ServerSocket socket = new ServerSocket(80);
????while(true) {
????final Socket conn = socket.accept();
????Runnable task = new Runnable() {
????public void run() {
????handleRequest(conn);
????}
????};
????new Thread(task).start();
????}
????}
????}
????????????????????????????????????????????????????????????????汾?????????????????????????????
????Executor ???
?????????????????????????????????????????е?????
????JDK ???? Executor ?????
????public interface Executor {
????void execute(Runnable command);
????}
??????? Executor ???????????????????????п?????????????????????????????????в????????????????????????????????????й???????????? Runnable ??????????Executor ?????????????????????????????????ó???????????
????Executor ???????????????????????????????????????????????????????????????
???????? Executor ?? WebServer ???????£?
????public class TaskExecutorWebServer {
????private static final int NTHREADS = 100;
????private static final Executor exec = Executors.newFixedThreadPool(NTHREADS);
????public static void main(String[] args) throws Exception {
????ServerSocket serverSocket = new ServerSocket(80);
????while (true) {
????final Socket conn = serverSocket.accept();
????Runnable task = new Runnable() {
????@Override
????public void run() {
????handleRequest(conn);
????}
????};
????exec.execute(task);
????}
????}
????}
????????????????? Executor ?????????????????е???????????
????/**
????* ????????? Runnable ????????
????* ???????????????????????????????е???????????????????????????????????????
????* ?????? Executor ??????????????????
????*
????*
????* @author renchunxiao
????*
????*/
????public class ExecutorDemo {
????public static void main(String[] args) {
????Executor executor = new ThreadExecutor();
????executor.execute(new Runnable() {
????@Override
????public void run() {
????// do something
????}
????});
????Executor executor2 = new SerialExecutor();
????executor2.execute(new Runnable() {
????@Override
????public void run() {
????// do something
????}
????});
????}
????}
????/**
????* ??????????????? command
????*
????* @author renchunxiao
????*
????*/
????class ThreadExecutor implements Executor {
????@Override
????public void execute(Runnable command) {
????new Thread(command).start();
????}
????}
????/**
????* ??????? command
????*
????* @author renchunxiao
????*
????*/
????class SerialExecutor implements Executor {
????@Override
????public void execute(Runnable command) {
????command.run();
????}
????}
????????
??????????????????????????? Executors ?е??????????????????????
????newFixedThreadPool????????????????????????????????????????????????????????????С????仯??
????newSingleThreadExecutor???????????
????newCachedThreadPool?????????????????????
????newScheduledThreadPool???????????????????????????????????????
????JVM ????????з???????????????????????????????????????? Executor????? JVM ?????????
???????????з?????????????????и???? Executor ???????? ExecutorService??
????public interface ExecutorService extends Executor {
????void shutdown();
????List<Runnable> shutdownNow();
????boolean isShutdown();
????boolean isTerminated();
????boolean awaitTermination(long timeout?? TimeUnit unit)
????throws InterruptedException;
????<T> Future<T> submit(Callable<T> task);
????<T> Future<T> submit(Runnable task?? T result);
????Future<?> submit(Runnable task);
????<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
????throws InterruptedException;
????<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks??
????long timeout?? TimeUnit unit)
????throws InterruptedException;
????<T> T invokeAny(Collection<? extends Callable<T>> tasks)
????throws InterruptedException?? ExecutionException;
????<T> T invokeAny(Collection<? extends Callable<T>> tasks??
????long timeout?? TimeUnit unit)
????throws InterruptedException?? ExecutionException?? TimeoutException;
????}
????ExecutorService ?????????????????????С????????????ExecutorService ?????????????????????shutdown ?????????????????????μ????????????????е???????????(??????Щ??δ?????????)??shutdownNow ?????????????????????????????????е???????????????????????δ????????????????????????????????????
????Callable ?? Future
????Executor ?????? Runnable ????????????????????Runnable ??????о????????????? run ????????????????????????????
?????????????????????????????????????????????????????????????Щ????Callable ?????????????? call ??????????????????????????
????Executor ??е????????????????????:?????????????????ɡ???????Щ??????????????п????????????? Executor ?????У?????δ?????????????????
????Future ???????????????????????????????????????ж????????????????????????????????????????