The difference between Runnable and Thread in Java

The start of the thread is not simply calling your RUN method, but a thread scheduler to call the RUN methods of all your threads respectively.
Our ordinary RUN method will not return if it is not executed, that is, it will Keep executing it, so the method below the RUN method cannot be executed, but the RUN method in the thread is different, it only has a certain CPU time, and it is given to other threads after execution, so that the CPU time is repeatedly used. Cut to and fro, because the switching speed is very fast, so we feel that many threads are running at the same time.

You simply call the run method has no such effect, so you must call the start method of the Thread class to start your thread .So you have two ways to start a thread.
One is to write a class that inherits from the Thread class, then rewrite the run method inside, and use the start method to start the thread.
The second is to write a class that implements the Runnable interface, implements the run method inside, and uses new Thread. (Runnable target).start() method to start

These two methods must implement the RUN method, so that when the thread starts, the thread manager can call your RUN method.

Your TestThread does not inherit from the Thread class, how could it be Is there a start method?



There are two ways to implement multithreading in java, one is to inherit the Thread class, the other is to implement the Runnable interface;

The Thread class is defined in the java.lang package. As long as a class inherits the Thread class and overrides the

The run() method can realize multi-threaded operation, but a class can only inherit one parent class, which is the limitation of this method.

Here's an example:

  package org.thread.demo;

  class MyThread extends Thread{

  private String name;

  public MyThread(String name) {

  super();

  this.name = name;

  }

  public void run(){

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

  System.out.println("线程开始:"+this.name+",i="+i);

  }

  }

  }

  package org.thread.demo;

  public class ThreadDemo01 {

  public static void main(String[] args) {

  MyThread mt1=new MyThread("线程a");

  MyThread mt2=new MyThread("线程b");

      // thread1, thread2, in order

  mt1.run();

  mt2.run();

  }

  }

  However, at this time the result is very regular, the first object executes first, then the second object executes, and does not run each other. It can be found in the JDK documentation that once the start() method is called, the run() method is found by the JVM. start below

  The start() method starts the thread:

  package org.thread.demo;

  public class ThreadDemo01 {

  public static void main(String[] args) {

  MyThread mt1=new MyThread("线程a");

  MyThread mt2=new MyThread("线程b");

  // out of order

      mt1.start();

  mt2.start();

  }

  };

      In this way, the program can normally complete the interactive operation. So why do you have to use the start() method to start multithreading?

  在JDK的安装路径下,src.zip是全部的java源程序,通过此代码找到Thread中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,那么这样的技术成为JNI技术(java Native Interface)

  ·Runnable接口

  在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口完成。

  public interface Runnable{

  public void run();

  }

  例子:

  package org.runnable.demo;

  class MyThread implements Runnable{

  private String name;

  public MyThread(String name) {

  this.name = name;

  }

  public void run(){

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

  System.out.println("线程开始:"+this.name+",i="+i);

  }

  }

  };


但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable target)

  此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多

  线程。(start()可以协调系统的资源):

  package org.runnable.demo;

  import org.runnable.demo.MyThread;

  public class ThreadDemo01 {

  public static void main(String[] args) {

  MyThread mt1=new MyThread("线程a");

  MyThread mt2=new MyThread("线程b");

  new Thread(mt1).start();

  new Thread(mt2).start();

  }

  }

  · 两种实现方式的区别和联系:

  在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比

  继承Thread类有如下好处:

  ->避免点继承的局限,一个类可以继承多个接口。

  ->适合于资源的共享

  以卖票程序为例,通过Thread类完成:

  package org.demo.dff;

  class MyThread extends Thread{

  private int ticket=10;

  public void run(){

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

  if(this.ticket>0){

  System.out.println("卖票:ticket"+this.ticket--);

  }

  }

  }

  };

  下面通过三个线程对象,同时卖票:

  package org.demo.dff;

  public class ThreadTicket {

  public static void main(String[] args) {

  MyThread mt1=new MyThread();

  MyThread mt2=new MyThread();

  MyThread mt3=new MyThread();

  mt1.start();//每个线程都各卖了10张,共卖了30张票

  mt2.start();//但实际只有10张票,每个线程都卖自己的票

  mt3.start();//没有达到资源共享

  }

  }

  如果用Runnable就可以实现资源共享,下面看例子:

  package org.demo.runnable;

  class MyThread implements Runnable{

  private int ticket=10;

  public void run(){

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

  if(this.ticket>0){

  System.out.println("卖票:ticket"+this.ticket--);

  }

  }

  }

  }

  package org.demo.runnable;

  public class RunnableTicket {

  public static void main(String[] args) {

  MyThread mt=new MyThread();

  new Thread(mt).start();//同一个mt,但是在Thread中就不可以,如果用同一

  new Thread(mt).start();//个实例化对象mt,就会出现异常

  new Thread(mt).start();

  }

  };

  虽然现在程序中有三个线程,但是一共卖了10张票,也就是说使用Runnable实现多线程可以达到资源共享目的。

  Runnable接口和Thread之间的联系:

  public class Thread extends Object implements Runnable

  发现Thread类也是Runnable接口的子类。

原文

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325758108&siteId=291194637