Home  >  Article  >  Java  >  Detailed explanation of the difference between Thread and runnable in Java multithreading

Detailed explanation of the difference between Thread and runnable in Java multithreading

黄舟
黄舟Original
2017-08-20 09:17:471891browse

This article mainly introduces relevant information about the difference between java multi-threaded Thread and runnable. There are two methods for java thread to inherit the thread class and implement the runnable interface. Examples are provided below to help everyone understand. Friends in need can refer to it

The difference between multi-threaded Thread and runnable

There are two ways to implement multi-threading in java: inheriting the Thread class and implementing the runnable interface

1, inherit the Thread class and override the parent class run() method


  public class thread1 extends Thread {
 
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是线程"+this.getId());
        }
    }
 
    public static void main(String[] args) {
        thread1 th1 = new thread1();
        thread1 th2 = new thread1();
        th1.run();
        th2.run();
    }
   }

The run() method is just an ordinary method, It is executed sequentially, that is, th2.run() is executed only after th1.run() is completed, so only one main thread is used for writing. Multi-threading is meaningless, so the start() method should be used to start the thread. The start() method will automatically call the run() method. The above code is changed to:


 public class thread1 extends Thread {
    
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是线程"+this.getId());
        }
    }
 
    public static void main(String[] args) {
        thread1 th1 = new thread1();
        thread1 th2 = new thread1();
        th1.start();
        th2.start();
    }
}

Start a new thread through the start() method. In this way, regardless of whether the run() method called by th1.start() has been executed, th2.start() will continue to be executed. If there is other code below, there is no need to wait for th2.start() to complete execution and continue execution. (The output thread ID is output randomly and alternately)

#2, implement the runnable interface


public class thread2 implements Runnable {
 
    public String ThreadName;
    
    public thread2(String tName){
        ThreadName = tName;
    }
    
    
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
    
    public static void main(String[] args) {
        thread2 th1 = new thread2("线程A");
        thread2 th2 = new thread2("Thread-B");
        th1.run();
        th2.run();
    }
}

Like Thread's run method, Runnable's run is just a normal method. In the main method, th2.run() must wait for th1.run() to complete before it can be executed. The program only uses one thread. For multi-threading purposes, you must also use Thread's start() method (runnable does not have a start method). The above code is modified to:


public class thread2 implements Runnable {
 
    public String ThreadName;
    
    public thread2(String tName){
        ThreadName = tName;
    }
    
    
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
    
    public static void main(String[] args) {
        thread2 th1 = new thread2("线程A");
        thread2 th2 = new thread2("Thread-B");
        Thread myth1 = new Thread(th1);
        Thread myth2 = new Thread(th2);
        myth1.start();
        myth2.start();
    }
}

Summary: 2 ways to implement java multi-threading, runable is an interface, thread is a class, runnable Only one run method is provided. It is recommended to use runable to implement java multi-threading. No matter what, you will eventually need to use thread.start() to make the thread in a runnable state.

The above is the detailed content of Detailed explanation of the difference between Thread and runnable in Java multithreading. 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