Java creates threads in two ways

  • 2020-05-12 02:33:01
  • OfStack

preface

Multithreading is often encountered in the process of our development, is also essential to master. The first thing we need to know when we know we need to do multithreaded development is how to implement multithreading, which is how we should create threads.

Creating a thread in Java is the same as creating a normal class object. We can create a thread in two ways:

1. Inherit the Thread class and override the run() method.
2. Implement Runnable interface and run() method.

Method 1: inherit the Thread class

The code is very simple

First, overload 1 constructor so that we can name the thread.
Override the run() method.
Here we start by having the thread output the thread name +start.
Then output the thread name +1 increment per 5ms.


/**
 * Created by holten.gao on 2016/10/17.
 */
public class threadThread extends Thread {
  public threadThread(String name) {
    super(name);
  }
  @Override
  public void run() {
    System.out.println(this.getName()+" start!");
    for(int i=0;i<10;i++){
      System.out.println(this.getName()+" "+i);
      try {
        Thread.sleep(5);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}

Method 2: implement the Runnable interface

The code is also very simple

Implement the run() method.
Here we start by having the thread output the thread name +start.
Then output the thread name +1 increment per 5ms.


/**
 * Created by holten.gao on 2016/10/17.
 */
public class runnableThread implements Runnable {
  @Override
  public void run() {
    System.out.println(Thread.currentThread().getName()+" start!");
    for(int i=0;i<10;i++){
      System.out.println(Thread.currentThread().getName()+" "+i);
      try {
        Thread.sleep(5);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}

The test results

The test code


/**
 * Created by holten.gao on 2016/10/17.
 */
public class Main {
  public static void main(String[] args) {
    Thread threadThread=new threadThread("threadThread");
    threadThread.start();
    Thread runnableThread=new Thread(new runnableThread(),"runnableThread");
    runnableThread.start();
  }
}

The test results


threadThread start!
threadThread 0
runnableThread start!
runnableThread 0
threadThread 1
runnableThread 1
threadThread 2
runnableThread 2
threadThread 3
runnableThread 3
threadThread 4
runnableThread 4
threadThread 5
runnableThread 5
threadThread 6
runnableThread 6
threadThread 7
runnableThread 7
threadThread 8
runnableThread 8
threadThread 9
runnableThread 9

The two methods are compared

1. Because Java only supports single inheritance, you can no longer inherit from other classes using method 1; Method 2 implements the interface without affecting the inheritance of other classes.
2. Method 1: since Thread is inherited, new can be start directly; Method 2 needs to pass the Thread object as a parameter to get the Thread object.
3. In method 1, the thread name can be directly obtained through this.getName; Method 2 requires Thread.currentThread ().getName ()


Related articles: