I recently used a lot of things about multithreading. Now I have some time to summarize the knowledge about multithreading. Welcome to shoot bricks.
There are four ways to create a thread:
First, inherit the Thread class, override the run method, instantiate the class, and call the start method to start the thread;
Second, implement the runnable interface and override the run method. Then, use the instance of this class as the target of the thread class to create a thread object and call the start method to start the thread;
Third, implement the callable interface and override the call method, and then use the futuretask package to implement the callable Interface Class Object, next, use the futuretask class instance as the Thread class target to create a thread and call the start method to start the thread;
Fourth, call the static method of executors to create a thread pool, and then use the submit method of the returned executorservice object to submit the runnable or callable implementation class instance to the thread pool for running.
Among the above four methods, the first method is the most basic, and the second and third Methods eventually create a Thread class object, the last method is to improve the performance through the thread pool.
The first method is to inherit the Thread class, which leads to the inability to inherit other classes. Therefore, the flexibility is inferior to the second method, and for resource sharing, the instance attributes defined in the subclass cannot be shared, and the security is safe, but it also brings inconvenience, and even loses the value of multithreading in a certain sense-sharing data.
The second method is usually used (^ o ^ I always use this method anyway), and the re-written run method is used as the thread execution body. When the start method is called, it is ready. This method is used to implement the runnable interface, so it makes up for Java's single-re-inheritance, but it is still used as a parameter of the thread class to run the thread by the Thread class instance, this also shows that the Thread class is used in Java to represent the thread, and all the thread objects must be instances of the thread class or its subclass.
The advantage of the third method is that a return value can be obtained after the thread is executed, and an exception can be declared and thrown (for the call method ), rewriting the run method does not allow such "Chaos ".
The last implementation method is very convenient and improves the performance. It is used in places where threads are frequently used.
Note:
1. You cannot start a thread by directly calling the run or call method. The effect of this method is the same as that of calling a common method. The method is executed in the main thread, instead of creating a subthread.
2. You can use the instance method getname of the thread class to get the name of a thread. When the thread name is not set through the setname method (or a string representing the thread name is not input when the thread is created) by default, the main thread is named "Main" and sub-threads are named "thread-0" and "thread-1". Note that, you can directly use the getname method when obtaining the name of the current thread in the subclass inherited from the Thread class. In other cases, you must use "thread. currentthread (). getname.
The next step is thread control. Use the following functions in Java to control the thread:
Static Methods in two thread classes
① Sleep ② yield
One instance method
① Join
Of course there are other methods, such as stop, suspend, and resume, but they are generally not used. Among the preceding three methods, the first method can block the currently running thread, so that other threads can be executed, the parameter of this method specifies the time when the thread is in the blocking state. After the time, the thread returns to the ready state and waits for the next execution. The yield method only puts the current thread in the ready state, this means that the current thread may still be executed. When the priority of other threads in the program is lower than that of the current thread, calling this method cannot achieve the expected effect. In this case, you can use the setpriority method to set the thread priority, that is to say, the yield method only gives the thread with a priority equal to or higher than the call thread for execution. Join
The method causes the calling thread to execute the code after the calling statement is completed.
Finally, thread security issues. Multithreading makes resource sharing more convenient. In particular, it uses the following three methods to create threads and ensures thread security. Random modification of shared data by multiple threads may cause serious program logic errors. This is because once a thread is created and started, the status of each thread is completely equal, it is not affected by the thread that created it. Java uses the following methods to ensure thread security:
① Synchronous Code block ② synchronous method ③ Synchronous lock
The synchronization code block can use any object as the synchronization Monitor, and the synchronization method uses the current object as the synchronization monitor by default. The synchronization lock makes thread-safe operations more flexible. In essence, the three methods are the same. They are used to lock access to competing resources first, and then lock access is completed so that other threads can obtain the lock to access the resource.
Of course, multithreading also involves deadlock, communication, and other content. If you are not familiar with it, you will not be playing a big game in front of Guan Gong. In short, multithreading makes the program structure more ideal and is an indispensable part of the program.