Four ways of multithreading-java thread creation

There are four methods of creating threads on the surface in java, but in fact it can be said that there are two

1. Through the Thread class, rewrite the run method. Call its start again.

2. Implement the Runable interface to rewrite the run method through the Thread class. Pass the Runable object into the constructor of the Thread class, and then call the start of the Thread class.

3. Through the Thread class, rewrite the run method through the FutureTask and Callable interfaces, pass the instance object into the Thread class construction method, and then adjust the start of the Thread class.

4. Create threads through ThreadPool. Callbale objects or Runable objects need to be submitted to the thread pool to control the timing of execution logic.

It can be seen that the essence is to play with Thread and thread pool. The following code is a practical example:

@Slf4j
public class Thread01 {
    public static void main(String[] args) throws Exception{
        //普通Thread
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                log.info("普通new Thread run");
            }
        };
        t1.start();

        //实现Runbale接口
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                log.info("实现runable接口 run");
            }
        };
        new Thread(runnable,"t2").start();;


        //实现callable接口
        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.info("实现callable接口 run");
                return 100;
            }
        });
        new Thread(task,"t3").start();
        log.info("执行结果:{}",task.get());

        //线程池方式
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(runnable);
        FutureTask<String> task1 = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "这是callable";
            }
        });
        executorService.submit(task1);
        log.info("线程池执行结果:{}",task1.get());
    }
}

in conclusion:

 Thread's native creation of threads is rare in actual projects, because its run logic can only be used by the current thread and has a strong binding relationship. Generally, Runable is used to implement the recording logic, and then passed to Thread. Then the Callable method is more return of execution results than Runable. Real code applications can be used for inter-thread communication, since execution results may be required between multiple threads. The way of thread pool is mainly to reduce context switching and make resources more flexible. The submit method is provided so that you don't need to pay attention to the internal implementation, you only need to submit the task. It is generally used for tasks such as large file data processing.

Guess you like

Origin blog.csdn.net/weixin_42740540/article/details/124120935