In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what are the three ways to achieve multithreading in Java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Introduction
The main role of multithreading is to make full use of the resources of cpu. Single-threaded processing, in the process of loading the file, the processor will be idle all the time, but it will also be added to the total execution time. The total time for serial execution is equal to one time per split * the number of strings after syncopation. It would be nice to execute the program and wait a few minutes to finish processing. While in the process of multithreading, file loading and difference,
1. Three ways of implementing multithreading with Java
1. Inherit Thread
Multithreading is implemented through Thread inheritance and overriding the run method, as shown in the following example:
Public class ThreadPattern extends Thread {@ Override public void run () {System.out.println ("inherit Thread current execution thread" + Thread.currentThread (). GetName ());}} / / Test public void threadTest () throws ExecutionException, InterruptedException {ThreadPattern pattern = new ThreadPattern (); pattern.start ();} 2. Implement the Runnable interface
The implementation class of Runable is used as the construction parameter of Thread to implement multithreading. The example is as follows:
Public class RunnablePattern implements Runnable {@ Override public void run () {System.out.println ("implement Runnable mode, current execution thread" + Thread.currentThread (). GetName ();}} / / Test public void runnableTest () throws ExecutionException, InterruptedException {RunnablePattern runnablePattern = new RunnablePattern (); Thread thread = new Thread (runnablePattern); thread.start ();} 3. Implement the Callable interface
Implementing the Callable interface rewrites the call () method, then wraps it as FutureTask, and then wraps it as Thread, which is essentially an implementation of the Runnable interface. The examples are as follows:
Public class CallablePattern implements Callable {@ Override public Object call () throws Exception {System.out.println ("implement Callable mode, current execution thread" + Thread.currentThread (). GetName ()); return "1";}} / / Test public void callableTest () throws ExecutionException, InterruptedException {CallablePattern callablePattern = new CallablePattern (); FutureTask futureTask = new FutureTask (callablePattern); new Thread (futureTask). Start () Second, summarize the comparison of the three modes of use
1. Thread: the way of inheritance, due to java's single inheritance mechanism. You cannot inherit other classes and are not flexible enough to use them.
2. Runnable: implement the interface, which is more flexible than the Thread class, and there is no restriction of single inheritance.
3, Callable:Thread and runnable both rewrite the run method and have no return value, Callable rewrites the call () method and has a return value, with the help of the FutureTask class to determine whether the thread is completed or cancel the thread execution, generally do not put the thread body code directly in the Thread class, generally start the thread through the Thread class.
The 4:Thread class implements Runnable, and Callable encapsulates FutureTask,FutureTask to implement RunnableFuture,RunnableFuture to implement Runnable, so Callable is also a kind of Runnable, so the way of implementation is essentially Runnable implementation.
This is the end of this article on "what are the three ways to achieve multithreading in Java?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.