Home  >  Article  >  Java  >  What are the ways to create threads in Java? Three ways to create threads in Java

What are the ways to create threads in Java? Three ways to create threads in Java

不言
不言Original
2018-09-25 15:49:144229browse

What this article brings to you is what are the ways to create threads in Java? The three ways to create threads in Java have certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Inherit Thread class creation

By inheriting Thread and rewriting its run(), the thread executes the task in the run method. The created subclass can execute thread methods by calling the start() method.

By inheriting the thread class implemented by Thread, instance variables of the thread class cannot be shared between multiple threads. (Need to create different Thread objects, naturally not shared)

Example:

/**
 * 通过继承Thread实现线程
 */
public class ThreadTest extends Thread{
  
  private int i = 0 ;

    @Override
    public void run() {
        for(;i<50;i++){
            System.out.println(Thread.currentThread().getName() + " is running " + i );
        }
    }

    public static void main(String[] args) {
        for(int j=0;j<50;j++){if(j=20){
                new ThreadTest().start() ;
                new ThreadTest().start() ;
            }
        }
    }
}

2. Create a thread class through the Runnable interface

This method needs to first define a class to implement the Runnable interface and rewrite the run() method of the interface. This run method is the thread execution body. Then create an object of the Runnable implementation class as the parameter target for creating the Thread object. This Thread object is the real thread object. Thread classes that implement the Runnable interface share resources with each other.

/**
 * 通过实现Runnable接口实现的线程类
 */
public class RunnableTest implements Runnable {
    private int i ;
    @Override
    public void run() {
        for(;i40f403edaffc1509d11c744747179c63 task = new FutureTaskc0f559cc8d56b43654fcbe4aa9df7b4a((Callablec0f559cc8d56b43654fcbe4aa9df7b4a)()->{
           int i = 0 ;
           for(;i<100;i++){
               System.out.println(Thread.currentThread().getName() + "的循环变量i的值 :" + i);
           }
           return i;
        });
       for(int i=0;i<100;i++){
           System.out.println(Thread.currentThread().getName()+" 的循环变量i : + i");
           if(i==20){
               new Thread(task,"有返回值的线程").start();
           }
       }
       try{
           System.out.println("子线程返回值 : " + task.get());
        }catch (Exception e){
           e.printStackTrace();
        }
    }
}

Summary

Through the above three methods, they can actually be classified into two categories: inheriting classes and implementing interfaces. Compared with inheritance, interface implementation can be more flexible and will not be limited by Java's single inheritance mechanism. And resources can be shared by implementing interfaces, which is suitable for multi-threads processing the same resource. Thread knowledge is rich and complicated, and more details need to be learned and mastered.

The above is the detailed content of What are the ways to create threads in Java? Three ways to create threads in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn