繁体   English   中英

如何在Java中并行执行具有不同输入的方法的多个实例?

[英]How to execute multiple instances of a method with different inputs in parallel in java?

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


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

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

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.
         */
    }
}

@ purtip31开始进行并行处理。

我担心结果-您提到要更新“全局列表”。 如果一次有多个线程试图同时更新该列表,则可能会出现问题。 有两个选择:

  1. 确保该列表正确是线程安全的。 这可能很容易,也可能不容易-取决于要更改的内容。
  2. 使用ExecutorService ,但使用invokeAll()方法,该方法并行运行一堆Callable,并等待它们全部完成。 然后,您可以浏览所有结果并一次更新一个 结果没有线程问题。 这意味着您的代码将必须实现Callable而不是Runnable(没什么大不了的)。 我有一个博客,这里有一个例子

好吧,山姆...我对您的问题没有很清楚.....

试试这个...。

以下是有助于您运行多个实例的代码。

主螺纹

    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()
   {


   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM