In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
            
                    
                
2025-11-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use multithreading and thread pools in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Inherit Thread
Inheriting Thread to execute tasks can indeed open a thread to execute tasks, if you often open some threads, it will also lead to a waste of system resources.
Public static class Mythread extends Thread {@ Override public void run () {System.out.println ("current thread" + Thread.currentThread (). GetId ()); int I = 10 Thread 2; System.out.println ("run result" + I);}} / / calling thread. Public static void main (String [] args) throws ExecutionException, InterruptedException {/ * * thread execution mode * / Mythread mythread = new Mythread (); mythread.start (); / start thread System.out.println ("main--end");} implement Runnale interface public static class MyRunable implements Runnable {@ Override public void run () {System.out.println ("current thread" + Thread.currentThread (). GetId ()) Int I = 10max 2; System.out.println ("running result" + I);}}
Call.
/ * runable startup mode * / MyRunable runable = new MyRunable (); new Thread (runable). Start (); System.out.println ("main--end"); Callable/** * Callable can allow return values * / public static class Callale01 implements Callable {@ Override public Integer call () throws Exception {System.out.println ("current thread" + Thread.currentThread (). GetId ()); int I = 10Uni2 System.out.println ("run result" + I); return I;}}
Call. Here you need to build futureTask with callable
/ * the startup method of callale * / FutureTask futureTask = new FutureTask (new Callale01 ()); / / return the result. Integer I = futureTask.get (); new Thread (futureTask) .start (); System.out.println ("return result is:" + I); thread pool
Thread pool is our java development, often use a way to open multi-thread, thread pool, to manage threads. It can save system resources. Usually we write some of the following configurations in some configuration classes
/ * seven parameters * corePoolSize: 1. Number of core threads [always exists]: after the thread pool is created. The number of threads ready. * maxinumPoolSize: 2 maximum number of threads * keepaliveTime: survival time. The maximum wait time for idle threads. * the unit of unit wait time * blockingQueue blocking queue. If there are a lot of tasks, they will be placed in the queue, and as long as there are threads idle, they will go to the queue to get them. * threadFactory: factory for threads. * RejectExecutionHandler: if the queue is full. According to the strategy we specified. Refuse to carry out the mission. * * / ThreadPoolExecutor executor = new ThreadPoolExecutor, new LinkedBlockingQueue (100), Executors.defaultThreadFactory (), new ThreadPoolExecutor.AbortPolicy (); 4 common thread pools.
1 newCachedThreadPool ()
Create a cacheable thread pool that can flexibly recycle idle threads if the length of the thread pool exceeds the processing needs. If there is no recycling. A new thread is created.
Executors.newCachedThreadPool ()
2.newFixedThreadPool (6)
Create a fixed size thread pool.
3 newScheduledThreadPool ()
The thread pool for scheduled tasks.
4.newSingleThreadExecutor ()
Executors.newSingleThreadExecutor (); Thank you for your reading! This is the end of this article on "how to use multithreading and thread pool in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





            
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.