ExecutorService

 

package one;

import java.util.concurrent.Callable;

public class Task implements Callable<Integer> {
    private int type;

    public Task(int type) {
        this.type = type;
    }

    @Override
    public Integer call() throws Exception {
        if (1 == type) {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return 1;
        } else if (2 == type) {
            try {
                Thread.sleep(20000);
                return 2;
            } catch (Exception e) {
                System.out.println("xxxxxxxx");
                e.printStackTrace();
            }

            return -2;
        } else if (3 == type) {
            throw new Exception("sxsxsx");
        }

        return -1;
    }
}

 

package one;

public class MyThread extends Thread {
    private int type;
    
    public MyThread(int type) {
        this.type = type;
    }

    @Override
    public void run() {
        System.out.println("MyThread run() type:" + type);
    }   

}

 

package one;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Bleach {

    public static void main(String[] args) {
        Task task1 = new Task(1);
        Task task2 = new Task(2);
        Task task3 = new Task(3);
        MyThread thread = new MyThread(4);

        ExecutorService es = Executors.newFixedThreadPool(1);

        try {
            Future<Integer> future1 = es.submit(task1);
            System.out.println("begin task1 run");
            // 获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行
            System.out.println(future1.get());
            System.out.println("end task1 run\n");

            // ---------------------------------------
            Future<Integer> future2 = es.submit(task2);
            System.out.println("begin first task2 run");
            try {
                // sleep to wait the task2 to run
                Thread.sleep(1000);
                System.out.println("after sleep");
            } catch (Exception e) {
                e.printStackTrace();
            }

            boolean result = future2.cancel(true);
            System.out.println("task2 to cancel");
            if (!result) {
                System.out.println(future2.get());
            }
            System.out.println("end first task2 run");

            // ---------------------------------------
            Future<Integer> future3 = es.submit(task3);
            System.out.println("begin first task3 run");
            System.out.println(future3.get());
            System.out.println("end first task3 run");

            // ---------------------------------------
            // As to run(), returned value is null, it means has no returned
            // value
            es.submit(thread);
        } catch (InterruptedException | ExecutionException e) {
            System.out.println("Here is an Exception:");
            e.printStackTrace();
        }

        System.out.println("111111");
        // after call shutdown, it will not receive new task. After all tasks
        // are finished, thread pool will exit.
        es.shutdown();
        // try to shutdown now, if has task, it will try to stop it and
        // shutdown, but may not successfully.
        // es.shutdownNow();
        System.out.println("2222222");
    }
}

begin task1 run
1
end task1 run

begin first task2 run
after sleep
task2 to cancel
end first task2 run
xxxxxxxx
begin first task3 run
java.lang.InterruptedException: sleep interrupted
Here is an Exception:
    at java.lang.Thread.sleep(Native Method)
    at one.Task.call(Task.java:24)
    at one.Task.call(Task.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.ExecutionException: java.lang.Exception: sxsxsx
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at one.Bleach.main(Bleach.java:46)
Caused by: java.lang.Exception: sxsxsx
    at one.Task.call(Task.java:33)
    at one.Task.call(Task.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
111111
2222222

posted @ 2016-03-07 14:06  牧 天  阅读(192)  评论(0编辑  收藏  举报