In computer programming, a basic concept is to control multiple tasks simultaneously. Many programming problems require the program to stop the work at hand, to deal with other problems, and then return to the main process. There are many ways to achieve this. At first, programmers with low-level knowledge of machines wrote "Interrupt service routines," and the main process was halted through hardware-level interrupts. Although this is a useful approach, the programmed procedures are difficult to transplant, creating another category of costly problems.
In some cases, interruptions are necessary for those tasks that are highly real-time. But there are a number of other problems that only require dividing the problem into separate running pieces of the program, allowing the entire program to respond more quickly to user requests. In a program, these stand-alone pieces are called threads, and the concept of programming with it is called multithreading. A common example of multithreaded processing is the user interface. With a thread, the user presses a button and the program responds immediately, rather than having the user wait for the program to complete the current task before it starts responding.
Initially, threads are just a tool for allocating processing time for a single processor. However, if the operating system itself supports multiple processors, each thread can be assigned to a different processor and truly enter the "parallel operation" state. From a programming language perspective, one of the most valuable features of multithreading is that programmers don't have to worry about how many processors to use. The program is divided logically into several threads, and if the machine itself has more than one processor installed, the program runs faster without any special tuning required.
According to the previous discussion, you may feel that threading is very simple. But you have to pay attention to a problem: sharing resources! If multiple threads are running at the same time, and they are trying to access the same resources, you may experience a problem. For example, two processes cannot send information to a single printer at the same time. To solve this problem, for those resources that can be shared (such as printers), they must enter a locked state during use. So a thread can lock a resource and then untie (release) the lock after it completes its task so that other threads can then use the same resources.
Java's multithreaded mechanism has been built into the language, which makes a potentially more complex problem easier. Support for multithreading is supported at the object level, so an execution thread can be expressed as an object. Java also provides a limited resource lockout scenario. It locks up the memory occupied by any object (memory is actually one of a variety of shared resources), so only one thread at a time can use a specific memory space. To achieve this, you need to use the Synchronized keyword. Other types of resources must be explicitly locked by the programmer, which usually requires the programmer to create an object that represents a lock that all threads must check when accessing that resource.