In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of calculating the number of matching files in java thread pool". The content in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the method of calculating the number of matching files in java thread pool"?
The cost of building a new thread is a bit high because it involves interaction with the operating system. If your program creates a large number of short-lived threads, you should use thread pools. A thread pool contains a large number of idle threads ready to run. You give a Runnable object to the thread pool, and a thread in the thread pool will call the run method. When the run method exits, the thread does not die, but continues to be in the pool ready to service the next request.
The Executor class has a large number of static factory methods for building thread pools, and the following table gives a summary.
Method describes newCachedThreadPool to create new threads when needed: idle threads will be reserved for 60 seconds the newFixedThreadPool pool contains a fixed number of threads; idle threads will always be retained as a "pool" of only one thread in newSingleThreadExecutor, which sequentially executes each submitted task newScheduledThreadPool's fixed thread pool built for scheduled execution, a single-thread "pool" built by newSingleThreadScheduledExecutor for scheduled execution.
The newCachedThreadPool, newFixedThreadPool, and newSingleThreadExecutor methods return an object of the ThreadPoolExecutor class, which implements the ExecutorService interface.
To submit a task to the thread pool, submit an object that implements the Runnable or Callable interface to ExecutorService:
Future submit (Runable task) Future submit (Runable task, T result) Future submit (Callable task)
The thread pool executes the submitted task as soon as possible, and when submit is called, a Future object is returned to query the status of the task or cancel it.
* A submit method submits a Runable object and returns a Future, which can be used to call isDone, cancel, or isCancelled to query the task status. But the get method of this Future object simply returns null when the task is completed.
The second version of the submit method also submits a Runable object, and the get method that returns Future returns the incoming result object when the task is completed.
The third submit method submits a Callable object, and the returned Future object will get it when the structure is calculated and ready.
When you want to log out of a thread pool, call the shutdown method, which starts the shutdown sequence for that thread pool. At this time, the thread pool is not immediately sacrificed and the thread is gone, but wait until all the tasks are completed, the thread in the thread pool will die, and the closed executor will no longer accept new tasks. You can also call shutdownNow, where the thread pool cancels the queued task and attempts to interrupt the executing thread.
Here is a summary of what you should do when using connection pooling:
Call the static newCachedThreadPool or newFixedThreadPool method in the Executor class.
Call submit to submit a Runnable or Callable object.
If you want to be able to cancel the task or if you submit a Callable object, save the returned Future object.
Call shutdown when you don't want to submit any more tasks.
In addition to the regular calculation of the number of matching files, this program prints out the number of threads in the pool during execution. But this information is not available from the ExecutorService interface. Therefore, we must transform the pool object into a ThreadPoolExecutor class object.
Import java.io.*; import java.util.*; import java.util.concurrent.*; public class ThreadPoolTest {public static void main (String [] args) throws Exception {Scanner in = new Scanner (System.in); System.out.print ("Enter base directory (e.g. / usr/local/jdk5.0/src):"); String directory = in.nextLine () System.out.print ("Enter keyword (e.g. Volatile):"); String keyword = in.nextLine (); ExecutorService pool = Executors.newCachedThreadPool (); MatchCounter counter = new MatchCounter (new File (directory), keyword, pool); Future result = pool.submit (counter); try {System.out.println (result.get () + "matching files.") } catch (ExecutionException e) {e.printStackTrace ();} catch (InterruptedException e) {} pool.shutdown (); int largestPoolSize = ((ThreadPoolExecutor) pool). GetLargestPoolSize (); System.out.println ("largest pool size=" + largestPoolSize);} / * * This task counts the files in a directory and its subdirectories that contain a given keyword. * / class MatchCounter implements Callable {/ * * Constructs a MatchCounter. * @ param directory the directory in which to start the search * @ param keyword the keyword to look for * @ param pool the thread pool for submitting subtasks * / public MatchCounter (File directory, String keyword, ExecutorService pool) {this.directory = directory; this.keyword = keyword; this.pool = pool;} public Integer call () {count = 0 Try {File [] files = directory.listFiles (); ArrayList results = new ArrayList (); for (File file: files) if (file.isDirectory ()) {MatchCounter counter = new MatchCounter (file, keyword, pool); Future result = pool.submit (counter); results.add (result) } else {if (search (file)) count++;} for (Future result: results) try {count+ = result.get () } catch (ExecutionException e) {e.printStackTrace ();}} catch (InterruptedException e) {} return count;} / * Searches a file for a given keyword. * @ param file the file to search * @ return true if the keyword is contained in the file * / public boolean search (File file) {try {Scanner in = new Scanner (new FileInputStream (file)); boolean found = false; while (! found & & in.hasNextLine ()) {String line = in.nextLine () If (line.contains (keyword)) found = true;} in.close (); return found;} catch (IOException e) {return false;}} private File directory; private String keyword; private ExecutorService pool; private int count } Thank you for your reading. The above is the content of "what is the method for calculating the number of matching files in java thread pool". After the study of this article, I believe you have a deeper understanding of what is the method for calculating the number of matching files in java thread pool, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.