A Java Question Every Day [10]

subject

The two most commonly used methods of creating threads and their comparison are described.

   

Answer

Method 1: Inheritance of Thread class implementation

Steps:

  1. Create subclasses of the Thread class, such as MyThread.
  2. Rewrite the run() method of the Thread class.
  3. Instantiate the MyThread class with object names such as myThread.
  4. Start threads with the start() method of the Thread class, such as myThread.start().

   

Method 2: Implementing Runnable Interface

Steps:

  1. Create a class, such as MyRunnableThread, to implement the Runnable interface.
  2. Create an object of the MyRunnableThread class.
  3. Instantiate the Thread class with object names such as thread and pass in two parameters, MyRunnableThread class and thread name, to its constructor.
  4. Start threads with the start() method of the Thread class, such as thread.start().

   

The difference between inheriting Thread class to create threads and implementing Runnable interface to create threads is that when using the same class to create multiple threads, the former actually creates several different Thread objects, and its internal run() methods are executed in their own objects without interference, just like multiple threads to perform multiple tasks; the latter actually uses the same class to create multiple threads. Objects create multiple threads, so the attributes within the object are common, which is equivalent to multiple threads performing the same task.

In addition, in use, classes that inherit the Thread class can not inherit other classes, while classes that implement the Runnable interface can also inherit other classes, the former is relatively limited.

This may be a bit abstract. Let's explain it in code.

   

Reference Code

   

MyThread class

package me.huangzijian;

public class MyThread extends Thread {

    private int num = 10;
    private String name;

    public MyThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        int count = num;
        for (int i = 0; i < count; i++) {
            System.out.println(this.name + ":" + num);
            num--;
        }
    }
}

   

MyRunnable class

package me.huangzijian;

public class MyRunnableThread implements Runnable {

    private int num = 10;

    @Override
    public void run() {
        int count = num;
        for (int i = 0; i < count; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + num);
            num--;
        }
    }
}

 

 

TheadCreation class

package me.huangzijian;

public class ThreadCreation {

    public static void main(String[] args) {
        // inherit Thread Class implementation
        MyThread myThread1 = new MyThread("MyThread1");
        MyThread myThread2 = new MyThread("MyThread2");
        MyThread myThread3 = new MyThread("MyThread3");
        myThread1.start();
        myThread2.start();
        myThread3.start();

        // Realization Runnable Interface
        MyRunnableThread myRunnableThread = new MyRunnableThread();
        Thread t1 = new Thread(myRunnableThread, "MyRunnableThread1");
        Thread t2 = new Thread(myRunnableThread, "MyRunnableThread2");
        Thread t3 = new Thread(myRunnableThread, "MyRunnableThread3");
        t1.start();
        t2.start();
        t3.start();
    }
}

 

Operation results:

After running ThreadCreation, we will see that inheriting the Thread class implements the following results:

As you can see, the three threads operate on the field num individually, reducing the num from 10 to 1.

The implementation of Runnable interface is as follows:

As you can see, three threads work together on num of the same myRunnableThread object. According to the program, each thread cycles 10 times, so num is reduced from 10 to negative. Some friends may ask why all three threads get 10 at the beginning. This is the problem of thread synchronization, which needs to be modified with keywords such as synchronized.

Keywords: Java

Added by jamest on Thu, 27 Jun 2019 23:54:52 +0300