Process: An application that executes a process on a processing machine, which is a dynamic concept that contains one or more threads.
Thread: is an entity of a process that is the basic unit of CPU dispatch and dispatch.
The life cycle of a thread:
New State (Start ())
Ready state (Start ())
Run state (run ())
Blocking state ()
Death status (Stop () or destory ())
If a thread executes a method such as sleep, suspend (hangs), and so on, the thread will enter a blocking state from the running state after the resource is lost. You can re-enter the ready state after the sleep time has arrived or the device resource has been acquired. Can be divided into three kinds:
-
Wait for blocking: the thread in the running state executes the wait () method, which causes the thread to enter a wait-blocking state.
-
Synchronous blocking: The thread acquires a synchronized synchronization lock failure (because the synchronization lock is occupied by another thread).
-
Other blocking: When an I/O request is made by the calling thread's sleep () or join (), the thread enters the blocking state. When the sleep () state times out, the join () waits for the thread to terminate or time out, or the I/O process completes and the thread is re-entered in a ready state.
Thread priority: The priority of a Java thread is an integer whose value range is 1 (thread.min_priority)-Ten (thread.max_priority).
By default, each thread will be assigned a priority of norm_priority (5).
Several important methods of Java threading
1.start () and run (): When implementing threading classes, either inheriting the thread class or implementing the Runnable interface, implement the run () method to implement the specific threading business logic, but be aware of using the start () method when actually starting the thread class, calling run ( ) method does not start a thread to execute, but only as a normal class method.
So the start () method starts 2 threads, and the run () method starts only one thread.
2.yield (): Pauses the currently executing thread object and executes other threads.
When the method is called, the thread immediately frees up the resource, and the CPU can move on to other threads. However, it is not certain which thread to execute, so it is also possible to execute the current thread the next time.
3.join (): Pauses the currently executing thread object and executes other threads.
4.sleep (): Allow the currently executing thread to hibernate (suspend execution) within the specified number of milliseconds
Methods for creating threads
1. By inheriting the thread class
Package Com.demo.thread;/*1. Create Multithreading: Inherit the Thread method rewrite run (): see the thread source code has a run method. Use Threads: Create subclass objects, objects. Start () startup * Disadvantages of inheriting the thread class: If a class has been inherited from a class, You can no longer inherit the thread class **/classThread1 extends thread{@Override Public voidrun () {//thread body, run everything from run start for(intI=0;i<5; i++) {System. out. println ("the rabbit ran away."+i+"Step"); } }}classThread2 extends thread{@Override Public voidrun () {//thread body, run everything from run start for(intI=0;i<5; i++) {System. out. println ("the Turtle ran away."+i+"Step"); } }} Public classthreadtest{ Public Static voidMain (string[] args) {//To create a child class objectThread1 thread1=NewThread1 (); Thread2 thread2=NewThread2 (); //call the Start method to start the threadThread1.start (); Thread2.start (); }}
2. Implement the Runnable interface
Package Com.demo.thread;/*Create a thread using runnable: Avoid the limitations of single inheritance, easy to share resources * 1. Class implementation runnable interface + rewrite run ()-----> Real Role class * 2. Start multi-threading, use static Proxy * 1) Create real character * 2) Create proxy role + real Role Reference * 3) called. Start () Start thread * * **/classRunna implements runnable{@Override Public voidrun () { for(intI=0;i< +; i++) {System. out. println ("while eating"); } } } Public classRunnabletest { Public Static voidMain (string[] args) {//1) Create a real roleRunna Runna =NewRunna ();//2) Create proxy role + real role referenceThread proxy =NewThread (Runna);//3) call. Start () Start threadProxy.start (); for(intI=0;i< +; i++) {System. out. println ("while playing with your phone"); } }}
Reference: Beginner's Tutorial
Detailed Threading Introduction
Java Thread Learning