How to create multiple threads

1. Inherited from the Thread class

1) Create a subclass that inherits from the Thread class

2) Rewrite the run() of the Thread class ---> declare the operations performed by this thread in run()

3) Create an object of a subclass of the Thread class

4) Call start() through this object

public class ThreadTest1{

   public static void main(String[] args){
         Thread1 test1 = new Thread1();
         Thread1 test2 = new Thread1();
         test1.start();
         test2.start();
         
 
}


}

class Thread1 extends Thread{

        //遍历100以内的整数
    @Override
    public void run(){

    for(int i = 0; i < 100; i++){

       //调用getName()方法显示当前线程名,方便观察
       System.out.println(Thread.currentThread().getName() + ":" + i)

}
  
}


}

2. Implement the Runnable interface

1. Create a class that implements the Runnable interface

2. Implement the class to implement the abstract method in Runable: run()

3. Implement the object of the implementation class

4. Pass this object as a parameter to the constructor of the Thread class to create an object of the Thread class

5. Call start() through the object of the Thread class

public class ThreadTest2{
       public static void main(String[] args){
           Thread2 thread2 = neww Thread2();
           Thresd test1 = new Thread(thread2);
           Thread test2 = new Thread(thread2);
           test1.start();
           test2.start();
           

}
  
}

class Thread2 implements Runnable{
  @Override 
  public void run(){
    for (int i = 0; i<100;i++){
         System.out.println(Thread.currentThread().getName() + ":" + i);

}

}


}

3. Implement the Callable interface

1. Create an implementation class that implements Callable

2. Implement the call method, and declare the operations that this thread needs to perform in call()

3. Create an object of the Callable interface implementation class

4. Pass the object of this Callable interface implementation class as a parameter to the constructor of FutureTask, and create an object of FutureTask

5. Pass the FutureTask object as a parameter to the constructor of the Thread class, create a Thread object, and call start()

6. Get the return value of the call method in Callable

How to understand that implementing the Callable interface to create multiple threads is more powerful than implementing the Runnable interface to create multiple threads?

1).call() can have a return value.

2) .call() can throw an exception and be caught by an external operation, so as to obtain the information of the exception

3). Callable supports generics

4. Use thread pool

Create multiple threads in advance and put them in the thread, get them directly when using them, and put them back into the pool after use.

1) Improved responsiveness (reduced time to create new threads)

2) Reduce resource consumption (reuse threads in the thread pool, do not need to create each time)

3) Easy thread management

   1) corePoolSize: the size of the core pool

   2) maximumPoolSize: maximum number of threads

   3) keepAliveTime: When the thread has no tasks, how long will it keep at most before it will be terminated

ExecutorService: The real thread pool interface.

Executors: tool class, factory class of thread pool, used to create and return different types of thread pools

5) Compare the first three methods

1) The implementation method does not have the limitation of single inheritance of classes

2. The implementation method is more suitable for dealing with the situation where multiple threads have shared data. It is said that the data needs to be static

Same point

Both methods need to rewrite run(), and declare the logic to be executed by the thread in run()

The problem with method 1 is that the current class needs to be inherited from the Thread class, and java is a single inheritance, so in order to create multi-threads, other classes cannot be inherited

Guess you like

Origin blog.csdn.net/weixin_58419099/article/details/131037183