Concurrent Programming canProgramSplit into independent tasks. With multithreading, each such independent task is driven by an execution thread. A thread is a separate sequential control flow in a process. A thread drives a task. You can use the runnable interface to describe a task. To define a task, you only need to implement the runnale and run () methods.
Calling the static method thread. Yield () in run () indicates a suggestion for thread schedule: The CPU time is transferred from one thread to the next thread.
Task run () does not have another independent thread driver, but directly calls it in main (in fact, this is also using a thread, that is, always main () A allocated thread ).
When a class inherits from runnable, it must implement run (), but this does not mean that it has the ability to run threads. To implement the thread execution capability, You need to explicitly bind this task to a thread.
/////////
The traditional method of converting a runnable object into a task is to pass it to the constructor of the thread.
A thread constructor only needs a runnable object. Calling the START () of a thread will execute necessary initialization work for the thread, and then call the run () method of the runnable object to start the task in a new thread.
Create a thread object in main (). It does not get the reference of the thread. For a common object, obtaining the reference of this object is helpful for subsequent garbage collection, but it is different for thread. Every thread is self-registered, so there is a reference to it in a certain place. Garbage collection will be executed only when the thread task exits run () or dies.
/////////
Executors is introduced in Java. util. Concurrent in Java se5 to help manage thread objects and simplify concurrent programming. Executors adds an indirect layer between the execution of the client and the task to execute the task through this indirect object, rather than directly executing the task through the client. Executos can manage the execution of asynchronous tasks without explicitly managing the thread lifecycle. An executorservice object knows how to build a proper context to execute runnable objects. Cachedthreadpool creates a thread for each new task and uses the static executors method to create the executorservice object. This determines the executor type.
Generally, a single executor can create and manage all tasks in the system. Shutdown () is called to prevent the new task from being submitted to this executor. Before Shutdown () is called, the current thread (that is, the thread driving main) will always run all submitted tasks. When all tasks in the executor are completed, the program will immediately exit.
The above example uses the cachedthreadpool type executor. You can also use other types of executors, such as fixedthreadpool. It uses a limited number of threads to execute submitted tasks. The executor of fixedthreadpool type is costly when being created, but it saves the cost of creating a new thread every time you run the task, the cachedthreadpool is used to create enough threads during program execution. When it can recycle the old thread, it stops creating new threads. Another type of executor is singlethreadexecutor. This type of executor is very useful when you need to run a long task, such as listening to sock connections, or run a small number of tasks in this thread. If multiple tasks are submitted to singlethreadexecutor, tasks are executed in a linear order.CompositionTo avoid asynchronous control, you can easily use it.
Note that in any thread pools, existing threads will be reused if possible.