(1) Threads and processes
In layman's terms, a process is an execution of an application on a processing machine, which is a dynamic concept, and a thread is part of a process that contains multiple threads running.
(2) Two ways to implement threading in Java, the first is to inherit the thread class, and implement the Runnable interface. Because Java is a single inheritance, it is more appropriate to use the second method when using multithreading.
(3) The life cycle of a thread
The state of the thread, respectively, is the state of birth, state of readiness, state of operation, waiting state, dormant state, blocking state, and death state. Birth status is the state of the thread creation time, before the user calls the start () method is the state of birth, when the user calls the start () method, the thread is in a ready state (executable state), when the thread gets the resources of the system will go into the running state. Once the thread has entered an executable state, it is converted in the ready state and running state, and it is possible to block, wait, hibernate, or be in a state of death. When a running thread calls the Wait () method, the thread enters the wait state, and the thread that enters the wait state must call the Notify () method in the thread class to be awakened. The Notifyall () method wakes all the threads that are in the waiting state. When you call the Sleep () method in the thread class, it goes into hibernation, and when the run () method of a thread finishes executing, the thread enters the dead state. So in the process of development we often set up loops in the run () method for time-consuming operations, whether it's a PC-side interface or an Android interface. In general, we use cyclic conditions to control whether the thread ends.
(3) thread blocking, synchronizing, interacting (communication)
Thread join, if there is a thread A, now you need to insert thread B, and require thread B to execute before proceeding with thread A, you can call the join method in the thread class to complete.
Threads have their own priority, which indicates the importance of the thread in the program, and if many threads are in a ready state, the system determines which thread executes first, based on the priority of the thread. But it does not mean that the priority bottom thread is not running, for example, the garbage collection thread has a very low priority.
Thread security issues, thread-safe problems originate from the fact that two threads access a single object data at the same time. The synchoronized keyword is generally used. The object data can be synchronously locked, or the method can be synchronized. This solves the problem of thread safety.
Java threads, processes, multithreading