Three ways to realize java multithreading are explained in detail

Recently, I learned and studied the use of Java multithreading when doing code optimization. After reading the opinions of rookies, I made a summary.

1. Inheriting thread class to implement multithreading

Although the method of inheriting thread class is listed as a multi-threaded implementation by me, thread is essentially an instance of the runnable interface, which represents an instance of a thread, and the only way to start a thread is through the start () instance method of thread class. The start () method is a native method that starts a new thread and executes the run () method. It is very simple to implement multithreading in this way. You can start a new thread and execute your own defined run () method by directly extending thread through your own class and copying the run () method. For example:

Start the thread in the appropriate place as follows:

2. Realize multithreading by implementing runnable interface

If your class already extends another class, you cannot directly extend thread. At this time, you must implement a runnable interface, as follows:

To start myThread, you need to instantiate a thread and pass in your own myThread instance:

In fact, when a runnable target parameter is passed to the thread, the run () method of the thread will call target Run(), refer to JDK source code:

3. Use executorservice, callable and future to realize multithreading with returned results

Executorservice, callable and future objects actually belong to the function classes in the executor framework. The framework is explained in detail here. The thread that returns the result is in jdk1 The new feature introduced in 5 is indeed very practical. With this feature, I don't need to make great efforts to get the return value, and even if it is implemented, it may be full of loopholes.

Tasks with a return value must implement the callable interface. Similarly, tasks without a return value must implement the runnable interface. After the callable task is executed, you can obtain a future object. On this object, you can call get to obtain the object returned by the callable task. Combined with the thread pool interface executorservice, you can realize the legendary multithreading with returned results. The following provides a complete multi-threaded test example with returned results in jdk1 5. It has been verified that there is no problem and can be used directly. The code is as follows:

Code Description:

The executors class in the above code provides a series of factory methods for creating the first thread pool, and all the returned thread pools implement the executorservice interface.

public static ExecutorService newFixedThreadPool(int nThreads)

Create a thread pool with a fixed number of threads.

public static ExecutorService newCachedThreadPool()

Create a cacheable thread pool. Calling execute will reuse previously constructed threads (if threads are available). If no existing threads are available, create a new thread and add it to the pool. Terminate and remove those threads that have not been used for 60 seconds from the cache.

public static ExecutorService newSingleThreadExecutor()

Create a single threaded executor. public static scheduledexecutorservice newScheduledThreadPool(int corePoolSize)

Create a thread pool that supports timed and periodic task execution. In most cases, it can be used to replace the timer class.

Executoreservice provides the submit () method, passes a callable or runnable, and returns the future. If the executor background thread pool has not completed the calculation of callable, this calls the get () method that returns the future object, which will block until the calculation is completed.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>