Thread discussion, multi-thread and asynchronous
I. There are two ways to implement the thread:One is to inherit the Thread class and override the run () method. This method has two disadvantages: 1. Because Java is a single inheritance, when a class inherits the Thread class, it cannot inherit other classes. 2. The run () method is rewritten inside the thread to define the task, which leads to a strong coupling between the thread and the task. The thread will become reusable and has low flexibility. The second is inheritance of the Runable interface. Calss RunnableTest implements Runnable {} RunnableTest r = new RunnableTest (); Thread t = new Thread (r); t. start (); Use (created using an anonymous internal class) Thread t = new Thread (new Runnable () {public void run () {implementation code}); t. conclusion: The start () method is to include threads in thread scheduling. When the CPU assigns a time segment to the thread, the run () method is automatically called to execute the code.Interview Questions:The running result is pongping. Conclusion: The start () method must be used for thread startup. If you call the run () method. In this case, a thread will not be started, which is equivalent to simply calling the run method 2. API for obtaining thread-related information: 1. get the current Thread object: Thread t = Thread. currentThread (); 2. thread id: long id = t. getId (); 3. thread name: String name = t. getName (); 4. thread priority: int priority = t. getPriority (); 3. Thread priority: the thread scheduling is uncontrollable. For a thread, 1. The length of the allocated time slice cannot be controlled. 2. The thread to which the time is allocated is uncontrollable. The thread can only passively allocate time segments, but cannot actively request to obtain time segments. Setting the thread priority can interfere with thread scheduling to the maximum extent. Theoretically, threads with a higher thread priority are allocated more time segments. Set: Thread t = new Thread () {override run () method}; t. setPriority (Thread. MIN_PRIORITY); 4. The static void sleep (long MS) method specifies the number of milliseconds for thread blocking that runs the current method. After timeout, the thread returns to the runnable status again, waiting for the allocated time segment.
Thread. sleep (5000 );
This method does not need to create threads and can be used in any method.
Thread. yield ();
// This method is used to get the time segment of the current thread. Static Method
5. Daemon thread (also known as background thread)
By default, all created threads are foreground threads. The background thread must be specified through a method to change the foreground thread to the background thread. The main difference between background threads and foreground threads is the end time. When all foreground threads in a process end, the process ends, regardless of whether the background threads in the process are running or not. Usage: t. setDaemon (true); Note: This setting must be set before the start () method.
Sat. Void join () method
This method will block the thread that calls this method. For example, thread a calls the join () method of thread B, and B. join (). Then thread a enters the blocking state, knowing that thread B is finished, thread a will continue to execute, which is equivalent to inserting thread B in thread. When the thread is inserted, thread B is executed. After thread B is finished, thread a is executed.
Synchronized) Premise: (Multiple Threads must see the same object) 1. multi-thread concurrency security issues Due to the uncertainty and uncontrollable nature of thread switching, multiple threads may encounter serious problems when operating the same resource, therefore, to solve the problem of "queuing for each task", synchonized (synchronization lock) is used.
2. method modified with synchronized
After a method is modified by synchronized, it is called a synchronous method and cannot be accessed by multiple threads at the same time. The locked object is the object of the current method.
3. modify the code block with synchronized
Effectively narrowing the synchronization scope can improve the concurrency efficiency. Use the synchronized block to narrow down the scope. Because all locks are current objects. Therefore, you need to specify the locked object and use synchronized (this) {} to implement it. Lock this code. Therefore, multiple threads cannot enter the code at the same time.
4. Use synchronized for static methods
When synchronized is used in static methods, the locked object is the class Object of the current class. Is the Class object. No matter where you call this method, you cannot enter the static method at the same time. (Multiple threads see the same object)
5. mutex lock
Two or more different synchronized objects in the same object
6. deadlock
Multiple objects