Summary: The following is a summary of some of the notes I made while learning Java threads and some of the information I consulted. Mostly in the form of questions and answers.
One, what is a thread?
A: A thread is a lightweight process, and now a basic dispatch unit in the operating system, and threads are executed independently of each other, the thread is essentially a running activity of data, and is a unit of operating system allocation resources and scheduling. Threads in Java are run in-process, just as the JVM is actually a Java process. There are cooperative, preemptive ways to invoke threads.
The resources between processes and processes are not shared, such as when we open a world document with a process, the second process may be able to view it, but it cannot be changed, and sometimes it cannot be viewed, which is common in Windows operating systems, such as opening a world, and want to delete this world will be prompted that this file has been occupied, is not deleted, this is a very large process of the characteristics of its resources are exclusive, can not be shared with each other.
While the thread runs inside the process, the resources that the process consumes, then the thread and the thread can be shared, then it will form the thread and thread to go to each other, the same resource situation, some things are not encountered in peacetime, the Java JVM in the background log, in fact, is a number of threads alternating to write, This is one example of resource sharing within a process, of course, the port number is also, Tomcat listens to the 8080 port number, the HTTP request returns, will start a thread, actually is a thread has been listening to it, actually can form a resource sharing situation, but in fact, If the second Tomcat listens on the same port number, this time it will be an error.
Threads are actually very resource-intensive,
Threads have context switches: even in a single-core CPU that supports multithreading, the CPU implements this mechanism by allocating time slices to each CPU. Time slices are CPU allocated to each thread of time, because the time is very short, so the CPU through the constant switching thread execution, the time slice is generally dozens of milliseconds, the CPU through the time slice allocation algorithm to perform the task, a time slice, will switch to the next task, but the switch will save the status of the previous task, From the task save to reloading process is a context switch, context switch, like reading, stop reading, write down the location to stop reading, and from the new reading will affect the speed of multi-threaded execution.
Kernel state: The CPU can also switch itself from one program to another by accessing all the data in memory, including peripherals such as hard drives, network cards, and the CPU.
User state: Only limited access to memory, and no access to peripheral devices, the ability to consume CPU is stripped, CPU resources can be obtained by other programs.
Java Thread day-01