The partition of Thread state:

There are six states of threads in Java.

  1. Start (NEW) : A NEW thread object is created, but the start() method has not been called yet.
  2. Runnable: The ready and running states of a Java thread are collectively called “running.” After the thread object is created, another thread (such as the main thread) calls the object’s start() method. The thread in this state is in the runnable thread pool, waiting to be selected by the thread scheduling, access to the CPU, and is in the ready state. A ready-state thread becomes running when it obtains a CPU slice.

3. Blocked: Indicates that the thread is blocking on the lock. Waiting: The thread that is in this state is WAITING for some particular action (notification or interrupt) from another thread. 5. Timeout wait (TIMED_WAITING) : This state is different from WAITING and can return itself after a specified time.

  1. Terminating: Indicates that the thread has finished executing.

These six states are defined in the State enumeration of Thread class, and you can check the source code for one-to-one correspondence.

1. Thread state diagram

Two, state detailed description

2.1. Initial state (NEW)

Implement the Runnable interface and inherit Thread to obtain a Thread class, new an instance of the Thread, the Thread into the initial state.

2.2. Runnable Ready

  1. Ready state just means you’re eligible to run, and if the scheduler (CPU) doesn’t pick you up, you’re always ready.
  2. Call the thread’s start() method, and the thread is in the ready state.
  3. The current thread’s sleep() method ends, other threads join() ends, wait for the user input is completed, one thread gets the object lock, these threads will also enter the ready state.
  4. When the current thread’s time slice is exhausted, the current thread’s yield() method is invoked, and the current thread enters the ready state.
  5. After the thread in the lock pool obtains the object lock, it enters the ready state.

2.3. Runnable RUNNING status

The state that a thread is in when the thread scheduler selects a thread from the runnable pool as the current thread. This is the only way a thread can enter a running state.

2.4. Blocked state

The blocking state is the state in which a thread blocks when it enters a method or block of code modified by the synchronized keyword (acquiring the lock).

2.5. Waiting for(WAITING)

Threads in this state are not allocated CPU execution time; they wait to be explicitly woken up, or they will be in an indefinite wait state.

2.6. Waiting over time(TIMED_WAITING)

Threads in this state are not allocated CPU execution time, but instead of waiting indefinitely for another thread to explicitly wake up, they will wake up automatically after a certain amount of time has been reached.

2.7. State of termination

  1. It is considered terminated when the thread’s run() method completes, or when the main thread’s main() method completes. The thread object may be alive, but it is no longer a separate executing thread. Once a thread is terminated, it cannot be resurrected.
  2. On a termination of the thread calls the start () method, which will be thrown. Java lang. IllegalThreadStateException anomalies.

Three, waiting for the queue

  • Before calling obj’s wait(), notify() methods, the obj lock must be acquired, which must be written in the synchronized(obj) code segment.
  • Steps and diagrams associated with waiting queues

  1. Thread 1 acquires the lock on object A and is using object A.
  2. Thread 1 calls the wait() method on object A.
  3. Thread 1 releases the lock on object A and immediately enters the wait queue.
  4. Objects in the lock pool compete for the lock of object A.
  5. Thread 5 acquires the lock on Object A, enters the synchronized block, and uses Object A.
  6. Thread 5 calls the notifyAll() method on object A to wake up all threads and enter the synchronization queue. If thread 5 calls the notify() method of object A, it wakes up A thread. It is unknown who will be awakened, but the awakened thread enters the synchronization queue.
  7. The notifyAll() method ends with synchronized, and thread 5 releases the lock on object A.
  8. Threads in the synchronous queue scramble for the object lock, but it is not known when thread 1 will be able to grab it.

Four, synchronous queue status

  • When the current thread tries to call A synchronized method on object A and finds that the lock on object A is held by another thread, the current thread enters the synchronization queue. In short, the synchronization queue is filled with threads that want to compete for the lock of an object.
  • When thread 1 is awakened by another thread 2, thread 1 enters the synchronization queue to fight for the object lock.
  • Synchronization queue is a concept that only exists in the context of synchronization, one object corresponds to a synchronization queue.
  • When a thread has waited or been woken up by notify/notifyAll, it enters the synchronization queue to compete for a lock, and if it gets the lock, it enters the Runnable state, or the Blocked state, waiting to acquire the lock.

V. Comparison of several methods

  1. Thread.sleep(long millis) : This method must be called by the current Thread. When the current Thread enters the state of TIMED_WAITING, but does not release the lock, the Thread automatically wakes up and enters the ready state. Effect: The best way to give another thread a chance to execute.
  2. Thread.yield() : This method must be invoked by the current Thread. The current Thread drops the CPU time slice, but does not release the lock resource. The current Thread changes from running state to ready state, allowing the OS to select the Thread again. Effect: alternate execution between threads of the same priority, but not guaranteed. In practice, there is no guarantee that yield() will yield, because the yielding thread may also be selected again by the thread scheduler. Thread.yield() does not cause a block. This method is similar to sleep(), except that the user cannot specify how long it pauses.
  3. Thread.join ()/ Thread.join (long millis), the current thread calls the join method of another thread T, the current thread enters the state of WAITING/TIMED_WAITING, the current thread does not release the lock of the object it already holds. When thread T finishes execution or Millis time has expired, the current thread typically enters the Runnable state or the Blocked state is also possible (because the Join is implemented based on Wait).
  4. Object.wait(), in which the current thread invokes the wait() method of the Object. The current thread releases the lock and enters the wait queue. Wake up by notify()/notifyAll() or wait(long timeout) timeout to automatically wake up.
  5. Object.notify() wakes up a single thread waiting on this Object monitor, and the choice is arbitrary. NotifyAll () wakes up all threads waiting on this object monitor.
  6. LockSupport.park()/LockSupport.parkNanos(long nanos),LockSupport.parkUntil(long deadlines), The current thread enters the WAITING/TIMED_WAITING state. In contrast to wait, you don’t need a lock to get a Thread into the WAITING/TIMED_WAITING state, and you need to wake it up with locksuppo.unpark (Thread Thread).

Several ways to create threads

Thread

The process of running a Thread generates a lot of information, which is stored in a member variable of the Thread class. Common examples are: a. The Thread’s ID is uniquely identified as getId() b. The Thread’s name: getName(), which defaults to “Thread-XX” c if not set. The priority of the thread is from 1 to 10. The higher the number, the higher the priority, and the greater the likelihood of getting the JVM schedule executed. The JDK has three common states built-in:

Public final static int MIN_PRIORITY = 1; Public final static int NORM_PRIORITY = 5; Public final static int MAX_PRIORITY = 10; public final static int MAX_PRIORITY = 10; `

It is generally not recommended to set the priority of a thread. An IllegalArgumentException will appear if it is set to an illegal priority.

Create Thread Method 1:

Inherit the Thread class. Step 1: Define a class that extends Thread. 2, override the run method in class Thread. 3. Create a subclass of Thread directly to create a Thread. 4. Call the start method to start the thread and call the thread’s task run method to execute.

You can get the name of the Thread by using the getName of the Thread. The name of the Thread is main.

Class Demo extends Thread {/** ** String */ private String name; Demo(String name) {// change thread name to super(name); //this.name = name; } //*** The definition in the run method is the task code for the thread to run. *** public void run() { for(int x=0; x<10; x++) { //for(int y=-9999999; y<999999999; y++){} System.out.println(name+".... x="+x+"..... name="+Thread.currentThread().getName()); } public static void main(String[] args) {D1 = new D2; Demo d2 = new Demo("xiaoqiang"); d1.start(); // Start the thread and call the run method. d2.start(); System.out.println("over...." +Thread.currentThread().getName()); }}

Create Thread Method Two

When the class has its own parent class, override the run method by implementing the Runnable interface. (commonly used) steps:

1. Define a class to implement the Runnable interface.

2. Overwrite the run method in the interface and encapsulate the task code of the thread into the run method.

Create a Thread object from Thread class and pass a subclass object of the Runnable interface as an argument to the constructor of Thread class.

Why is that?Because the tasks of the thread are encapsulated in the run method of the Runnable subclass object.

So when the thread object is created, it must be clear what task to run.

Thought:The task of the thread is encapsulated as an object through the Runnable interface.

4. Call the start method of the thread object to start the thread.

Benefits of implementing the Runnable interface:

1. Separate the task of the thread from the subclass of the thread for separate encapsulation, and encapsulate the task into an object according to the idea of object-oriented. 2. It avoids the limitation of Java single inheritance.

//extends Fu //extends the Demo class so that its contents can be executed as tasks for threads. // This is done as an interface. class Demo implements Runnable{ public void run() { show(); } public void show() { for(int x=0; x<20; x++) { System.out.println(Thread.currentThread().getName()+"....." +x); } } } class ThreadDemo { public static void main(String[] args) { Demo d = new Demo(); Thread t1 = new Thread(d); Thread t2 = new Thread(d); t1.start(); t2.start(); }}

Create Thread Mode 3

Implement the Callable interface

Callable is more powerful than using Runnable

1 can have a return value compared to the run() method

2 methods can throw an exception

Support for generic return values

4 You need to use the FutureTask class, for example, to get the results returned

The Future interface

1. You can cancel the execution results of specific Runnable and Callable tasks, check whether the query is completed, get the results, etc.

FutrueTask is the only implementation class of the Futrue interface

3 FutureTask also implements Runnable and Future interfaces. It can either be executed by the thread as a Runnable or as a Future to get the return value of the Callable

// Class Stu implements Callable {//2. Class Stu implements Callable {//2. @Override public Object Call () throws Exception {int sum=0; for (int i = 1; i <=100; i++) { if(i % 2 == 0){ System.out.println(i); sum += i; } } return sum; } } public class Bank { public static void main(String[] args) { //3. Create Callable interface implementation class object Stu Stu = new Stu(); //4. Pass the object of this Callable interface implementation class to the FutureTask constructor as the object to create FutureTask futureTask = new FutureTask(stu); //5. Start () new Thread(futureTask).start(); try { Object sum = futureTask.get(); System.out.println(" sum "+sum); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }}}

Create Thread Mode 4

Use a thread pool context: Resources that are often created and destroyed and are particularly heavily used, such as concurrent threads, can have a significant impact on performance.

Idea: create a number of threads in advance, into the thread pool, use direct access, use back to the pool. Frequent creation and destruction can be avoided and repeated use can be realized. It’s like a public communication tool in life.

Benefits:

1. Improved response speed (reduced time to create new threads);

Reduce resource consumption (reuse threads in the thread pool, do not need to create each time);

3. Easy to manage threads;

CorePoolSize: Core pool size MaximumPoolSize: Maximum number of threads keepAliveTime: Maximum length of time a thread can hold without a task before terminating
Class MyThread implements Runnable {@Override public void run() {for (int I = 1; i <= 100; i++) { if(i % 2 ==0){ System.out.println(Thread.currentThread().getName() + ":" + i); } } } } public class ThreadPool { public static void main(String[] args) { // 1. Specify the number of threads ExecutorService service = Executors. NewFixedThreadPool (10); ThreadPoolExecutor service1= (ThreadPoolExecutor) service; //service1.setMaximumPoolSize(15); //service1.setCorePoolSize(); */ / 2. Pass the object of the Runnable implementation class to the Submit () method of the ExecutorService. Start the thread and execute(run() service.execute(new MyThread()); // apply to Runnable //service.submit(); CALLABLE // 3. End the thread using service.shutdown(); }}