Java Create thread pool comparison of two different methods

Source: Internet
Author: User

Use Java to do crawl when it is unavoidable to use multithreading, because to crawl multiple sites or a thread to crawl a site is too slow, and sometimes a thread crawl the same site is also more wasteful CPU resources.   To use multithreading, and so on, it is unavoidable to control the thread or use the thread pool. When I was doing our current crawl framework, we used Java.util.concurrent.ExecutorService as a thread pool, and the code for Executorservice was probably as follows:

The Java.util.concurrent.Executors Class API provides a number of static methods for creating connection pools:

1. Fixed size of the thread pool:

 package backstage;  
  Import java.util.concurrent.Executors;  
       
  Import Java.util.concurrent.ExecutorService; public class Javathreadpool {public static void main (string[] args) {//Create a thread pool of reusable fixed thread count E  
         Xecutorservice pool = Executors.newfixedthreadpool (2);  
         Creating a Runnable interface object, thread object of course also implements the Runnable interface thread T1 = new Mythread ();  
         Thread t2 = new Mythread ();  
         Thread t3 = new Mythread ();  
         Thread T4 = new Mythread ();  
         Thread T5 = new Mythread ();  
         Put the thread into the pool for execution pool.execute (T1);  
         Pool.execute (T2);  
         Pool.execute (T3);  
         Pool.execute (T4);  
         Pool.execute (T5);  
     Close the thread pool Pool.shutdown (); } class Mythread extends Thread {@Override public void run () {System.out.println ( Thread.CurrentThread (). GetName () + "is executing ...  
     "); }  
 }

The

later discovered that Executorservice's functionality was not as good as it was imagined, and that it provided a container of only one thread, So then I used the java.lang.threadgroup,threadgroup has a lot of advantages, the most important thing is that it can traverse the thread, know that those threads have been running, and those threads are running. The code of use for Threadgroup is as follows:

<span style= "FONT-SIZE:18PX;" ><span style= "Background-color:rgb (255, 255, 255);"  
       
   >class Mythread extends Thread {Boolean stopped;  
     Mythread (Threadgroup TG, String name) {super (TG, name);  
   stopped = false;  
     public void Run () {System.out.println () Thread.CurrentThread (). GetName () + "starting.");  
         try {for (int i = 1; i < 1000; i++) {System.out.print (".");  
         Thread.Sleep (250);  
         Synchronized (this) {if (stopped) break; Exception exc {System.out.println ()}} catch (Thread.CurrentThread (). GetName () + "interrupted  
     .");  
     }  
   System.out.println (Thread.CurrentThread (). GetName () + "exiting.");  
   } synchronized void Mystop () {stopped = true;  } public class Main {public static void main (String args[]) throws Exception {Threadgroup TG = new THREadgroup ("My Group");  
     Mythread thrd = new Mythread (TG, "Mythread #1");  
     Mythread thrd2 = new Mythread (TG, "Mythread #2");  
       
     Mythread thrd3 = new Mythread (TG, "Mythread #3");  
     Thrd.start ();  
     Thrd2.start ();  
       
     Thrd3.start ();  
       
     Thread.Sleep (1000);  
       
     System.out.println (Tg.activecount () + "threads in thread group.");  
     Thread thrds[] = new Thread[tg.activecount ()];  
     Tg.enumerate (thrds);  
       
     for (Thread t:thrds) System.out.println (T.getname ());  
       
     Thrd.mystop ();  
           
     Thread.Sleep (1000);  
     System.out.println (Tg.activecount () + "threads in TG.");  
   Tg.interrupt (); }}</span></span>

From the above code can be seen: Threadgroup than executorservice more than the following several advantages

1.ThreadGroup can traverse threads to know that those threads have run out and those that are still running

2. The number of threads that can be controlled by Threadgroup.activecount to control how many threads are inserted

This article comes from the blog park: http://www.cnblogs.com/yy2011/archive/2011/05/05/2037564.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.