Directory of this document
1 Threads and multithreading
What is a thread?
A thread is an object. For what? A Java thread (also known as a JVM thread) is a task that allows multiple simultaneous tasks within a Java process. The concurrent tasks within the process become threads (thread), and at least one thread in a process.
Java programs use multithreading to support a large number of concurrent request processing, if the program is executed in multi-threaded mode, its complexity is much higher than single-threaded serial execution. So many threads: it means that this program (a process) is running with more than one thread.
Why use multithreading?
Suitable for multi-core processors. A thread runs on a processor core, so multiple threads can be assigned to multiple processor cores, making better use of multicore processors.
Prevent blocking. Using multithreaded technology (or Message Queuing) to speed up the processing of code logic and shorten response time is not a strong data consistency operation.
Talk to multi-threading, probably talk about concurrency and parallelism, how to understand and distinguish between the two differences?
Like a single CPU, through the CPU scheduling algorithm, etc., the ability to handle multiple tasks, called concurrency
Similar to multiple CPUs, and the ability to process the same multiple tasks, called parallel
2 running and creating a thread
2.1 Creation of Threads
There are two ways in which Java creates thread objects:
Create a new MyThread object with the following code:
/*** inheriting the thread class creating threaded objects * @author Jeff Lee @ bysocket.com* @since January 27, 2018 21:03:02*/public class MyThread extends thread {@Override//can omit public void run () {System.out.println ("MyThread thread object is executing task");} public static void Main (string[] Arg s) {for (int i = 0; i < i++) {MyThread thread = new MyThread (); Thread.Start (); System.out.println ("MyThread Thread Object" + Thread.getid ());}}}
The MyThread class inherits the thread object and overrides the Run method to implement the logic inside the thread. The main function is to use the For statement, looping through the creation of 10 threads, calling the Start method to start the thread, and finally printing the ID of the current thread object.
What is the difference between the Run method and the Start method?
The Run method is meant to run, and the Run method is called after the thread has started.
The Start method is the starting point, which is to start a new thread instance. The thread's Run method is not tuned until the thread is started.
After executing the main method, the console prints as follows:
MyThread thread object is executing task MyThread thread object 10MyThread thread object is executing task MyThread thread object 11MyThread thread object is executing task MyThread thread objects 12MyThread Thread object that is executing task mythread thread object 13MyThread thread object is performing task Mythread thread object 14MyThread thread object is executing task mythread thread objects 15MyThread Thread object that is executing task mythread thread object 16MyThread thread object is performing task Mythread thread object 17MyThread thread object is executing task mythread thread objects 18MyThread Thread object that is executing the task Mythread thread object 19
As can be seen, the thread ID is a thread-unique identifier, and each thread ID is not the same.
Relationship between the Start method and the Run method:
Similarly, implementing the Runnable interface class to create thread objects is also simple, just a different form. The new Mythreadbrother code is as follows:
/*** implement Runnable interface class Create thread object * @author Jeff Lee @ bysocket.com* @since January 27, 2018 21:22:57*/public class Mythreadbrother Impl Ements Runnable {@Override//can omit public void run () {System.out.println ("Mythreadbrother thread object is executing task");} public static void Main (string[] args) {for (int i = 0; i < i++) {thread thread = new Thread (new Mythreadbrother ()); thread.st Art (); System.out.println ("Mythreadbrother Thread Object" + Thread.getid ());}}}
Specific code: "java-concurrency-core-learning"
Https://github.com/JeffLi1993/java-concurrency-core-learning
2.1 Running a thread
After running the two small demos, the JVM executes the main function thread and then executes the new thread in the main thread. Normally, all threads are executed until the end of the run. Unless System.exit (1) is called in a thread, it is terminated.
In real-world development, a request-to-response is a thread. In this thread, however, a new thread can be created by using thread pool to perform the task.
3 Status of the thread
To create a new Mythreadinfo class, Print thread object properties with the following code:
/*** property values for thread instance objects * @author Jeff Lee @ bysocket.com* @since January 27, 2018 21:24:40*/public class Mythreadinfo extends Thread {@ Override//can omit public void run () {System.out.println ("Mythreadinfo thread instance is executing task"),//System.exit (1);} public static Voi D main (string[] args) {mythreadinfo thread = new Mythreadinfo (); Thread.Start (); System.out.print ("Mythreadinfo Thread object" + "thread Unique identifier:" + Thread.getid () + "+" Thread Name: "+ thread.getname () +" "+" thread state: "+ Thread.getstate () + "+" Thread Priority: "+ thread.getpriority ());}}
The execution code prints as follows:
Mythreadinfo thread instance is executing task mythreadinfo thread-Object thread Unique identifier: 10 thread name: Thread-0 thread state: New Thread Priority: 5
A thread is an object that has a unique identifier ID, name, status, priority, and so on. A thread can only modify properties such as its priority and name, and cannot modify the ID or state. The ID is assigned by the JVM, and the default name is Thread-xx,xx is a set of numbers. The initial state of the thread is NEW.
The thread priority range is 1 to 10, where 1 is the lowest priority and 10 is the highest priority. Changing the priority of a thread is not recommended, and if the business requires it, it can naturally modify the thread priority to the highest, or lowest.
The state implementation of the thread is implemented by the Thread.state constant class, with 6 thread states: New (new), runnnable (operational), blocked (blocking), Waiting (wait), time waiting (timed wait), and Terminated (termination). The status transition diagram is as follows:
The thread state flow is roughly as follows:
After the thread is created, enter the new state
Call the start or Run method to enter the runnable state
The JVM performs runnable-state threads, such as thread priority and time fragmentation. Enter running state when starting execution
If the thread performs sleep, wait, join, or enter IO blocking, and so on. Enter wait or blocked status
When the thread finishes executing, the thread is removed by the thread queue. Finally, the terminated state.
4 Summary
This article introduces the basic thread and multithreading, including thread initiation and thread state. Next we talk about the specific operation of the thread. Including interrupts, terminations, etc.
Specific code: "java-concurrency-core-learning"
Https://github.com/JeffLi1993/java-concurrency-core-learning
Recommend an Exchange Learning Group:650385180 inside will share some senior architect recorded video recording: Spring,mybatis,netty source analysis, high concurrency, performance, distributed, micro-service architecture principle, JVM performance Optimization These become the necessary knowledge systems for architects. You can also receive free learning resources, and now benefit from:
2018 threaded and multithreaded interviews must know what is required