Inherit Thread and implement Runnable

If a class inherits Thread, it is not suitable for resource sharing. But if the Runable interface is implemented, it is easy to achieve resource sharing.

The advantages of implementing the Runnable interface over extending the Thread class:

1): Suitable for multiple threads of the same program code to process the same resource

2): The limitation of single inheritance in java can be avoided

3): Increase the robustness of the program, the code can be shared by multiple threads, the code and data are independent

Looking directly at the code, the following are:

1. Inherit the demo of Thread

2. Implement the demo of Runnable

package com.multithread.learning;
/**
 * Multi-threaded learning, inheriting Thread, resources cannot be shared
 *@author
 */
class Thread1 extends Thread{
	private int count=5;
	private String name;
    public Thread1(String name) {
       this.name=name;
    }
	public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(name + "运行  count= " + count--);
            try {
                sleep((int) Math.random() * 10);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
        }
       
	}
}

public class Main {

	public static void main(String[] args) {
		Thread1 mTh1=new Thread1("A");
		Thread1 mTh2=new Thread1("B");
		mTh1.start();
		mTh2.start();
	}
}
/**
 * Multi-threaded learning to achieve runnable, resources can be shared
 *@author
 */
package com.multithread.runnable;
class Thread2 implements Runnable{
    private int count=15;
	@Override
	public void run() {
		  for (int i = 0; i < 5; i++) {
			  System.out.println(Thread.currentThread().getName() + "运行  count= " + count--);
	            try {
	            	Thread.sleep((int) Math.random() * 10);
	            } catch (InterruptedException e) {
	                e.printStackTrace ();
	            }
	        }
	}
	
}
public class Main {

	public static void main(String[] args) {
		Thread2 mTh = new Thread2();
	        new Thread(mTh, "C").start();//The same mTh, but not in Thread, if the same instantiated object mt is used, an exception will occur   
	        new Thread(mTh, "D").start();
	        new Thread(mTh, "E").start();
	}
}//It should be noted here that each thread uses the same instantiated object. If it is not the same, the effect will be the same as above!

Remind everyone: the main method is actually a thread. In java, all threads are started at the same time. As for when, which one executes first, it depends entirely on who gets the CPU resources first.

In java, at least 2 threads are started per program run. One is the main thread and the other is the garbage collection thread. Because every time a class is executed using the java command, a jvm is actually started, and each jvm is actually a process started in the operating system.

The implementation in the second Main can be written through the Runnable interface, that is, two threads share a Thread2 instance, which achieves the purpose of resource sharing, but this cannot be achieved by inheriting Thread.

Guess you like

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