java执行器框架

发布于 2024-10-21 12:14:53 字数 1102 浏览 2 评论 0原文

请看我下面的代码....

  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); 

   }

//在这部分添加

      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.sleep(200);

      } 

      catch (InterruptedException e)

      {

      e.printStackTrace();

      }

      }

     }

如果结果集(rs)有100条记录执行器创建100个线程.....我需要在10内运行这个过程线程。我需要您的帮助来了解如何控制线程。如果任何线程已完成其任务,那么它应该处理结果集中的立即可用任务。是否可以使用执行器框架来实现。

谢谢...

vijay365

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.sleep(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尾戒 2024-10-28 12:14:53

您已经发布的代码可以执行此操作。您的代码不会立即生成 100 个线程。它将生成 10 个线程,这些线程消耗包含 Runnables 的队列中的任务。

来自 执行器.newFixedThreadPool Javadocs

创建一个重用线程池
一组固定的线程运行
共享无界队列。

您应该动态确定线程数,而不是使用静态线程数(本例中为 10):

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

另外,我不明白您为什么要调用 Thread.sleep?

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?

洋洋洒洒 2024-10-28 12:14:53

ResultSet 可能是一个 JDBC 查询结果。

这种设计几乎注定会失败。

JDBC 接口实现不是线程安全的。

结果集是稀有资源,应在创建它们的同一范围内关闭。如果你传递它们,你就是在自找麻烦。

多线程代码很难写好,如果不正确,调试起来就更困难。

这种设计几乎肯定是走错了方向。我敢打赌,你犯了过早优化的罪责。您希望多个线程将使您的代码更快,但会发生的情况是在一个 CPU 上进行 10 个线程时间切片,并花费相同或更长的时间。 (上下文切换也需要时间。)

更好的想法是将 ResultSet 加载到对象或集合中,关闭 ResultSet,然后对返回的对象执行一些多线程处理。

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.

你丑哭了我 2024-10-28 12:14:53

尝试 executor.submit(worker);

Try executor.submit(worker);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文