Understanding Threads
This time in reading "Java Concurrency programming Practical" this book, so that the Java multi-threading understanding and deepen some, feel that they can summarize, this article on the thread-related content it. We want to use threads, first of all we need to understand the thread, a short time ago I was talking about the operating system this article mentions a bit about threading, and interested classmates can read it. One thing to understand is that although we often say "Java multithreading", but actually thread this thing is supported by the operating system , it is not supported by the Java itself, so the actual implementation of the online process is platform-related! The students who have seen the object source code should be able to notice that the wait ()of the control thread implemented in the object class, thenotify () method, are local methods that are modified by the native keyword. It also indicates that these methods are platform-dependent, so we actually implement the thread-waiting wake-up mechanism by calling wait (), notify () by using the C language to invoke the interface provided by the operating system (personal understanding, if there are errors). The operating system also involves a user-state and kernel-state switch when executing the system call, which includes saving the program execution state and copying the data, which consumes some resources. And when the number of threads is large, the context switching time of the system also increases, so that too many threads will slow down the system's response. In fact, I think the Java application is just the abstraction and encapsulation of the threads in the operating system, which is one of the reasons why Java can achieve platform independence.
How threads are implemented in Java (three types)
Java is an almost entirely object-oriented language, and at first it was too superficial to understand the object in Java, so many phenomena could not be understood. After so long study, feel oneself to the object of understanding more deeply. Now let's talk about threads from the object's point of view. In Java, each thread instance is an object instance, which also conforms to the idea that everything is an object . So, the question of how to create a thread becomes the question of how to create an object , and our manipulation of that thread is the manipulation of that object. How to create an object this problem I think in Java should not be considered a problem! Creating an object is, of course, created by a class, which is the thread class. This is the first way to create a thread-inheriting the thread class. But the downside of this approach is that Java allows only one class to inherit, and if the class has inherited the thread class, it can no longer inherit another class. Thus Runnable , the way to implement the interface, but the object that implements the interface is not a thread object, it is just a thread can be executed by the object, if you want to start this thread, you also need to pass the object to a thread object to execute it, such as through the thread class construction method- public Thread(Runnable r) . So what is the third way, by implementing an interface that is Callable Runnable similar to implementing an interface, but Callable the call() method in can throw an exception and can have a return value .
About the thread pool
A thread pool framework is provided in the Java.util.concurrent package, and the implementation of the JDK is certainly great, and Doug Lea's work is naturally breathtaking, but sometimes the chicken is slaughtered sledgehammer. As long as the rationale is clear, it is not difficult to implement a lightweight thread pool yourself. Here is my own implementation of a Java thread pool, there are a lot of imperfect places, we can simply refer to. The essence of the thread pool is to hold two collections, a collection of tasks, and a collection of threads. Threads in the thread collection constantly pull the task out of the task collection and execute, and the thread does not end, and continues to wait for the task to execute. In my implementation, the task collection chooses the blocking queue so that it does not need to judge the status of the queue to wait or wake the thread that added the task or removed the task. So what is the benefit of using a thread pool? First, the thread pool avoids the overhead of constantly creating and destroying threads. Second, the use of the thread pool avoids the problem that the program creates a large number of threads and causes the system context switching time to increase. Third, because each thread needs to occupy a certain amount of memory, when the number of threads is too large, it can lead to insufficient memory, and the thread pool avoids the problem.
Summarize
Concurrency is really a problem, it causes our program to always have unexpected results, making programming more complex, but the concurrency does in some way improve the efficiency of the program. As long as we understand the principles and obey the rules, the concurrent program will do what we think.
Threads in Java