开发者

java-Executor Framework

开发者 https://www.devze.com 2023-02-16 10:12 出处:网络
Please look at my following code.... private static final int NTHREDS = 10; ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);

Please look at my following code....

  private static final int NTHREDS = 10;
  ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
  while(rs.next()){
            webLink=rs.getString(1);
            FirstName=rs.getString(2);
            MiddleName=rs.getString(3);
            Runnable worker = new MyRunnable(webLink,FirstName,MiddleName);// this interface has run method....
            executor.execute(worker); 

   }

//added

      public class MyRunnable implements Runnable  {


      MyRunnable(String webLink,String FirstName,String MiddleName){
         ** Assigning Values...***
      }

      @Override

      public void run() {

      long sum = 0;

      **Calling method to crawl by passing those Values**

      try {

      Thread.s开发者_JAVA百科leep(200);

      } 

      catch (InterruptedException e)

      {

      e.printStackTrace();

      }

      }

     }

In this part if the resultset(rs) having 100 records excutor creating 100 threads..... I need to run this process with in 10 threads. I need your help to know how to get control of threads.. If any thread has completed its task then it should process the immediate available task from the Result Set. Is it possible to achieve using executor framework.

Thanks...

vijay365


The code you've already posted does this. Your code will not immediately spawn 100 threads. It will spawn 10 threads that consume tasks from a queue containing your Runnables.

From the Executors.newFixedThreadPool Javadocs:

Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue.

Instead of using a static number of threads (10 in this case) you should determine the number dynamically:

    final int NTHREADS = Runtime.getRuntime().availableProcessors();

Also, I don't get why you are calling Thread.sleep?


ResultSet is probably a JDBC query result.

This design is almost certain to be doomed to failure.

The JDBC interface implementations are not thread-safe.

ResultSets are scare resources that should be closed in the same scope in which they were created. If you pass them around, you're asking for trouble.

Multi-threaded code is hard to write well and even harder to debug if incorrect.

You are almost certainly headed in the wrong direction with this design. I'd bet a large sum of money that you're guilty of premature optimization. You are hoping that multiple threads will make your code faster, but what will happen is ten threads time slicing on one CPU and taking the same time or longer. (Context switching takes time, too.)

A slightly better idea would be to load the ResultSet into an object or collection, close the ResultSet, and then do some multi-threaded processing on that returned object.


Try executor.submit(worker);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号