Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to implement multithreading in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what are the Java multithreading implementations", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "what are the Java multithreading implementations".

Good programmer Java training to share Java multi-threaded implementation, Java multi-threaded implementation of three main ways: inheritance Thread class, implementation of Runnable interface, the use of ExecutorService, Callable, Future implementation of multi-threading with return results. The first two methods have no return value after thread execution, only the last one is with return value. As follows:

1. Inheriting Thread class to realize multithreading

Inheriting methods from the Thread class, although I list it as a multithreaded implementation, Thread essentially implements an instance of the Runnable interface, represents an instance of a thread, and the only way to start a thread is through the Thread class's start() instance method. The start() method is a native method that starts a new thread and executes the run() method. This way to achieve multithreading is very simple, through their own class directly extend Thread, and override the run() method, you can start a new thread and execute their own defined run() method. For example:

[Java] view plain copypublic class MyThread extends Thread {

public void run() {

System.out.println("MyThread.run()");

}

}

Start the thread in the appropriate place as follows:

[Java] view plain copyMyThread myThread1 = new MyThread();

MyThread myThread2 = new MyThread();

myThread1.start();

myThread2.start();

2. Implement Runnable interface to realize multithreading

If your class has extended another class, you cannot directly extend Thread. In this case, you must implement a Runnable interface, as follows:

[Java] view plain copypublic class MyThread extends OtherClass implements Runnable {

public void run() {

System.out.println("MyThread.run()");

}

}

To start MyThread, you need to instantiate a Thread and pass in your own MyThread instance:

[Java] view plain copyMyThread myThread = new MyThread();

Thread thread = new Thread(myThread);

thread.start();

In fact, when a Runnable target parameter is passed to Thread, the Thread run() method calls target.run(), see JDK source code:

[Java] view plain copypublic void run() {

if (target != null) {

target.run();

}

}

3. Use ExecutorService, Callable, Future to implement multithreading with return results

ExecutorService, Callable, Future objects are actually function classes in the Executor framework. The framework is explained in great detail here. The thread that returns the result is a new feature introduced in JDK 1.5, and it's really useful, because with this feature I don't have to go through the trouble of getting the return value, and even if I did, it might be full of bugs.

Tasks that return values must implement the Callable interface, and similarly tasks that do not return values must implement the Runnable interface. After executing Callable task, you can get a Future object. Call get on this object to get the Object returned by Callable task. Combined with thread pool interface ExecutorService, you can realize the legendary multithreading with return results.

The above is "Java multi-threaded implementation of what" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report