【问题标题】:How to execute multiple instances of a method with different inputs in parallel in java?java - 如何在java中并行执行具有不同输入的方法的多个实例?
【发布时间】:2012-06-20 20:37:38
【问题描述】:

我有一个方法,它接受一个列表并对其进行一些处理,然后更新另一个全局列表。我需要使用不同的列表输入并行运行此方法的多个实例。 多线程支持吗?如果是,我该如何使用它,即:我应该在线程中放入什么?非常感谢示例。


我正在考虑在线程类中有一个静态列表,该列表在运行时由线程的不同实例更新(该列表包含字符串和计数器,因此更新是添加新字符串或增加现有字符串的计数器) ..我需要每 10 秒读取一次添加到此全局列表中的任何内容并打印它.. 使用适合此的静态列表以及如何使其线程安全?

【问题讨论】:

  • 我建议使用队列,而不是另一个线程必须每 10 秒轮询一次的静态列表。您的各种处理线程可以将结果放入队列中,单个线程将它们拉出并更新全局。

标签: java multithreading parallel-processing


【解决方案1】:

是的,这是多线程编程的一种非常常见的用法。

class ListProcessor implements Runnable {
    /* field/s representing param/s */
    public ListProcessor(/* param/s */) {
        /* ... */
    }

    @Override
    public void run() {
        /* process list */
    }
}

然后,当您想要实际处理一些列表时。

class SomeClass {
    ExecutorService listProcessor;
    public SomeClass(/* ... */) {
        listProcessor = ExecutorService.newFixedThreadPool(numThreads);
        /* for each thread, however you want to do it */
        listProcessor.execute(new ListProcessor(/* param/s */));
        /* when finished adding threads */
        listProcessor.shutdown();
        /* note that the above two lines of code (execute/shutdown) can be
         * placed anywhere in the code. I just put them in the constructor to
         * facilitate this example.
         */
    }
}

【讨论】:

    【解决方案2】:

    @purtip31 开始进行并行处理。

    我很担心结果——你提到你更新了一个“全局列表”。如果多个线程同时尝试更新该列表,则可能会出现问题。几个选项:

    1. 确保该列表是正确的线程安全的。这可能不容易,也可能不容易 - 取决于具体发生了什么变化。
    2. 使用ExecutorService,但使用invokeAll() 方法,该方法并行运行一堆Callable 并等待它们全部完成。然后,您可以查看所有结果并一次更新一个。结果没有线程问题。这意味着您的代码必须实现 Callable 而不是 Runnable(没什么大不了的)。 I have a blog with an example here

    【讨论】:

      【解决方案3】:

      嗯,山姆……我对你的问题不太清楚……

      试试这个....

      以下是帮助您运行多个实例的代码......

      主线程

          public class mainprocess
          {
      
                public static LinkedList globallist;
                public static String str;
                public int num;
                public static void main(String Data[])
                { 
                       globallist = new LinkedList();
                       // LinkedList will be passed as pass by reference.....
                       // globalist is made static and assigned likewise for global use..
                       childprocess.assignlist(globallist);
                       childprocess p1 = new childprocess("string input");  // a string input...
                       childprocess p2 = new childprocess(number input);   // a number input...
      
      
                       p1.t.join();
                       p2.t.join();
                 }
           }
      

      子线程.....

           public class childprocess implements Runnable
           {
                     public Thread t1,t2;
                     public boolean inttype,stringtype;     
                     String string;
                     int num;
                     public static LinkedList temp = new Linkedlist();
                     public static assignlist(LinkedList ll)
                     {
                             temp = ll;
                     }
                     public childprocess(String str)
                     {
                              string = str;
                              t1 = new Thread(this,"stringThread");
                              t1.start();
                     }
      
                     @override
                     public childprocess(int n)
                     {
                               num = n;
                               t2 = new Thread(this,"numberThread");
                               t2.start();
                     }
      
                     @override 
                     public void run()
                     {
                               // Both will be executed in a threader manner based on the condition...
                               if(Thread.currentThread().getName().equals("stringThread")
                               {
                                     // your process using string......
                                     childprocess.temp.add(str);
                               }
                               else if(Thread.currentThread().getName().equals("numberThread")
                               {
                                       // your process using number.....
                                       chilprocess.temp.add(num);
                               }
                      }                              
      
              }         
      

      如果您使用的函数一次只能使用一个线程...

      包括语法....

         public synchronized func_type func_name()
         {
      
      
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        • 2015-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多