【JUC】Callable interface

Use of the Callable interface

//比较两个接口
//实现Runnable接口
class MyThread1 implements Runnable {
    
    

    @Override
    public void run() {
    
    

    }
}

//实现Callable接口
class MyThread2 implements Callable {
    
    


    @Override
    public Integer call() throws Exception {
    
    
        System.out.println(Thread.currentThread().getName() + " 进入Callable");
        return 200;
    }
}


public class Demo1 {
    
    
    public static void main(String[] args) throws ExecutionException, InterruptedException {
    
    
        new Thread(new MyThread1(), "AA").start();
        // new Thread(new MyThread2(), "BB").start();  报错

        //FutureTask -->Runnable接口的实现类
        FutureTask<Integer> futureTask1 = new FutureTask<>(new MyThread2());

        //lam表达式
        FutureTask<Integer> futureTask2 = new FutureTask<>(()->{
    
    
            System.out.println(Thread.currentThread().getName() + " 进入Callable");
            return 300;});

        new Thread(futureTask2, "AA").start();
//        new Thread(futureTask1, "BB").start();
        while (!futureTask2.isDone()) {
    
    
            System.out.println("wait......");
        }
        System.out.println(futureTask2.get());
        System.out.println(futureTask2.get());//只计算一次

//        System.out.println(futureTask1.get());

        System.out.println(Thread.currentThread().getName() + "over");
        //FutureTask原理  未来任务
        /**
         * 举例:
         * 1.老师上课口渴了没水喝,跑去买水不合适,讲课线程继续,单开启另外线程,让班长去买水,把水买回来放那,老师需要的时候直接get()
         * 2。三个同学,三个计算任务,我是主线程需要统计他们计算的结果, 假设第二个同学计算量很大,
         *   我从第一个同学开始汇总,到了第二个同学,他还没计算好, 我单独给第二个同学开一个线程让他继续计算,我去汇总第三个同学。
         *   等我汇总完了第三个同学, 再回过头来等第二个同学计算完,并汇总。
         *
         * 汇总一次
         */
    }
}

Principle of FutureTask

For example:
1. The teacher is thirsty and has no water to drink in class. It is not appropriate to go to buy water. The lecture thread continues. Just open another thread, let the monitor buy water, buy it back and put it there. When the teacher needs it, get() directly.
2. Three students, three computing tasks, I am the main thread and need to count the results of their calculations. Assuming that the second classmate has a lot of computation, I start the summary from the first classmate, to the second classmate, he has not yet After the calculation is done, I will open a separate thread for the second student to let him continue the calculation, and I will summarize the third student. When I have finished summarizing the third classmate, I will go back and wait for the second classmate to finish the calculation and sum up.

The difference between the Runable interface and the Callable interface

  1. Whether there is a return value, Callable has a return value
  2. Whether an exception is thrown, if the result cannot be calculated, an exception will be thrown
  3. The implementation method names are different, the Runable interface is the run method, and the Callable interface is the call method

If you want to create a thread through the Callable interface, you must pass the implementation class FutureTask of the Runnable interface. The FutureTask construction can pass the Callable, and then new Thread, put it into the FutureTask

Guess you like

Origin blog.csdn.net/weixin_44179010/article/details/123342240