The state of the thread
Thread status diagram:
illustrate:
Threads include the following 5 states.
1. New state : After the thread object is created, it enters the new state. For example, Thread thread = new Thread().
2. Runnable: Also known as "executable state". After the thread object is created, other threads call the object's start() method to start the thread. For example, thread.start(). A thread in a ready state may be scheduled to execute by the CPU at any time.
3. Running state: The thread obtains CPU permissions for execution. It should be noted that threads can only enter the running state from the ready state.
4. Blocked state: Blocked state means that the thread gives up the CPU usage rights for some reason and temporarily stops running. It is not until the thread enters the ready state that it has a chance to go to the running state. There are three types of blockage:
(1) Waiting to block - By calling the thread's wait() method, let the thread wait for the completion of a certain work.
(2) Synchronized blocking--A thread fails to acquire synchronized synchronization lock (because the lock is occupied by other threads), it will enter a synchronized blocking state.
(3) Other blocking--The thread will enter a blocking state by calling sleep() or join() of the thread or issuing an I/O request. When the sleep() state timed out, join() waited for thread to terminate or timed out, or I/O processing was completed, the thread re-entered to the ready state.
5. Dead state: The thread has finished executing or exited the run() method due to an exception, and the thread ends its life cycle.
The contents involved in these 5 states include the Object class, Thread and synchronized keywords. We will learn these contents one by one in the following chapters.
Object class, defines sleep/wake functions such as wait(), notify(), notifyAll(), etc.
Thread class defines some column thread operation functions. For example, sleep() sleep function, interrupt() interrupt function, getName() get thread name, etc.
synchronized is a keyword; it is distinguished by synchronized code blocks and synchronized methods. The function of synchronized is to allow threads to acquire the synchronization lock of the object.
When we introduce wait(), notify() and other methods in detail later, we will analyze why "wait(), notify() and other methods should be defined in the Object class, not Thread class."
Two ways to implement multi-threading: Thread and Runnable
Runnable is an interface that only contains one run() method. Its definition is as follows:
public interface Runnable { public abstract void run();} The role of Runnable, implements multi-threading. We can define a class A to implement the Runnable interface; then create a new thread through new Thread(new A()) and other methods.
Thread is a class. Thread itself implements the Runnable interface. Its statement is as follows:
public class Thread implements Runnable {}
The role of Thread is to realize multi-threading.
Similarities and differences between Thread and Runnable:
The similarity between Thread and Runnable: both are “multi-threaded implementation methods.”
The differences between Thread and Runnable:
Thread is a class, and Runnable is an interface; Thread itself is a class that implements the Runnable interface. We know that "a class can only have one parent class, but it can implement multiple interfaces", so Runnable has better scalability.
In addition, Runnable can also be used for "sharing of resources". That is, multiple threads are created based on a certain Runnable object, and they will share resources on the Runnable object.
Generally, it is recommended to implement multi-threading through "Runnable"!
Multithreading examples for Thread and Runnable
1. Multithreading example of Thread
Below we can better understand Thread and Runnable through examples, and draw on an example online that is more convincing. // ThreadTest.java source code
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(this.getName()+" Tickets: ticket"+this.ticket--); } } } } } };public class ThreadTest { public static void main(String[] args) { // Start 3 threads t1, t2, t3; each thread sells 10 tickets! MyThread t1=new MyThread(); MyThread t2=new MyThread(); MyThread t3=new MyThread(); t1.start(); t2.start(); t3.start(); } } Running results:
Thread-0 Ticket Selling: ticket10Thread-1 Ticket Selling: ticket10Thread-2 Ticket Selling: ticket10Thread-1 Ticket Selling: ticket9Thread-0 Ticket Selling: ticket9Thread-1 Ticket Selling: ticket8Thread-2 Ticket Selling: ticket9Thread-1 Ticket Selling: ticket7Thread-0 Ticket Selling: ticket8Thread-1 Ticket Selling: ticket6Thread-2 Ticket Selling: ticket8Thread-1 Ticket Selling: ticket5Thread-0 Ticket Selling: ticket7Thread-1 Ticket Selling: ticket4Thread-2 Ticket Selling: ticket7Thread-1 Ticket Selling: ticket3Thread-0 Tickets: ticket6Thread-1 Tickets: ticket2Thread-2 Tickets: ticket6Thread-2 Tickets: ticket5Thread-2 Tickets: ticket4Thread-1 Tickets: ticket1Thread-0 Tickets: ticket5Thread-2 Tickets: ticket3Thread-0 Tickets: ticket4Thread-2 Tickets: ticket2Thread-0 Tickets: ticket3Thread-0 Tickets: ticket1Thread-0 Tickets: ticket2Thread-0 Tickets: ticket1Thread-0 Tickets: ticket2Thread-0 Tickets: ticket1
Results description:
(1) MyThread inherits from Thread, which is a custom thread. Each MyThread sells 10 tickets.
(2) The main thread main creates and starts 3 MyThread child threads. Each child thread sells 10 tickets.
2. Multithreaded example of Runnable
Next, we modify the above program. Implement an interface through Runnable, thereby implementing multi-threading.
// RunnableTest.java source code 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(Thread.currentThread().getName()+" Ticket selling: ticket"+this.ticket--); } } } } }; public class RunnableTest { public static void main(String[] args) { MyThread mt=new MyThread(); // Start 3 threads t1, t2, t3 (they share a Runnable object), and these 3 threads sell a total of 10 tickets! Thread t1=new Thread(mt); Thread t2=new Thread(mt); Thread t3=new Thread(mt); t1.start(); t2.start(); t3.start(); } } Running results:
Thread-0 Ticket Selling: ticket10Thread-2 Ticket Selling: ticket8Thread-1 Ticket Selling: ticket9Thread-2 Ticket Selling: ticket6Thread-0 Ticket Selling: ticket7Thread-2 Ticket Selling: ticket4Thread-1 Ticket Selling: ticket5Thread-2 Ticket Selling: ticket2Thread-0 Ticket Selling: ticket3Thread-1 Ticket Selling: ticket1
Results description:
(1) Unlike the above "MyThread inherits from Thread"; here MyThread implements the Thread interface.
(2) The main thread main creates and starts 3 child threads, and these 3 child threads are all created based on "mt, Runnable object". The result of the operation is that these 3 child threads sold a total of 10 tickets. This means that they share the MyThread interface.