First, take a look at the running status of each stage of the java thread running
A thread is an entity in a process and is the basic unit that is independently scheduled and dispatched by the system. The thread itself does not own the system resources, but only has a little resource that is essential during operation, but it can share the process with other threads that belong to the same process. All resources you have. One thread can create and undo another thread, and multiple threads in the same process can be executed concurrently. Due to the mutual constraints between threads, threads appear intermittent during operation.
In operating systems that introduce threads, processes are usually used as the basic unit for allocating resources, and threads are used as the basic unit for independent operation and independent scheduling. Since threads are smaller than processes and basically do not own system resources, the overhead of scheduling will be much smaller, which can more efficiently improve the degree of concurrent execution between multiple programs in the system, thereby significantly improving the system Resource utilization and throughput.
Threads are a single sequential control process in a program. Running multiple threads simultaneously in a single program to complete different work, called multithreading.
Multithreading is mainly used to save CPU time and utilize it. The threads need to use the computer's memory resources and CPU during the operation.
Multithreading is to complete multiple tasks simultaneously, not to improve operational efficiency, but to improve resource usage efficiency to improve system efficiency. Threads are implemented when multiple tasks need to be completed at the same time.
There are two ways to implement multi-threading in Java
1. Inherit the Thread class
2. Implement the Runnable interface
What are the common points of these two methods:
No matter which method is used, you must use Thread (if it is a Thread subclass, use it itself) to generate the thread, and then call the start() method.
The differences between the two methods:
1. One disadvantage of inheriting Thread class is single inheritance, while implementing the Runnable interface makes up for its shortcomings, and multiple inheritance can be achieved.
2. If you inherit the Thread class, if you generate a Runnable instance object, you must generate multiple Runnable instance objects, and then use Thread to generate multiple threads; and to implement the Runnable interface, you only need to establish an instance that implements this class and then use this instance. The object generates multiple threads. That is, the resource sharing is realized
Based on the above two points, it is recommended to use the second method
The following is an example to illustrate
Program 1:
package com.dr.runnable1;//A class inherits the Thread class, then this class is a multi-thread class. MyThread extends Thread{ private String name; public MyThread(String name) { this.na me=name; } //If To use multithreading, you must have the body of a method public void run() { //Printout for(int i=0;i<10;i++) { System.out.println(this.name+"--- ->Run,,,, "); } }}public class Demo1 { public static void main(String args[]) {//The first method Runnable r1=new MyThread("Thread A"); Runnable r2=new MyThread("Thread B"); Runnable r3=new MyThread("Thread C"); Thread t1=new Thread(r1); Thread t2=new Thread(r2); Thread t3=new Thread(r3); t1.start (); t2.start(); t3.start(); // mt1.run();// thread execution, use the start method// mt2.run();// mt3.run();// Two methods // MyThread mt1=new MyThread("Thread A");// MyThread mt2=new MyThread("Thread B");// MyThread mt3=new MyThread("Thread C");// mt1.start ();// mt1.start();// thread can only start once// mt2.start();// mt3.start(); } }The running results of the program are:
This is inheriting the Thread class, the first method generates multiple Runnable instance objects, and then uses Thread to generate multiple threads
The second method is that because this class has inherited the Thread class, it can directly use it to generate multiple threads.
Program 2:
package com.dr.runnable1;class MyThread1 implements Runnable{ private int ticket=10; public void run() { for(int i=0;i<500;i++) { if(this.t icket>0) { System.out .println("Ticket----->"+(this.ticket--)); } } }} public class Demo2 { public static void main(String args[]) { MyThread1 mt=new MyThread1(); : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : Thread t1=new Thread(mt); Thread t2=new Thread(mt); Thread t3=new Thread(mt); t1.start(); t2.start(); t3.start(); } }Program running results:
This program implements Runnable, generates a class of instance objects, and then uses Thread to generate multiple threads.