1

リストを取得して処理を行い、別のグローバルリストを更新するメソッドがあります。このメソッドの複数のインスタンスを、異なるリスト入力を並行して実行する必要があります。マルチスレッドはこれをサポートしていますか? はいの場合、どのように使用できますか。つまり、スレッドに何を入れますか? 例は高く評価されています。


実行中にスレッドのさまざまなインスタンスによって更新されるスレッドクラスに静的リストを作成することを考えています (リストには文字列とカウンターが含まれているため、更新により新しい文字列が追加されるか、既存の文字列のカウンターが増加します)。 10 秒ごとにこのグローバル リストに追加されたものをすべて読み取り、それを出力する必要があります。

4

3 に答える 3

6

はい、これはマルチスレッド プログラミングの非常に一般的な使用法です。

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.
         */
    }
}
于 2012-06-20T20:43:13.097 に答える
2

@ purtip31 は、並列処理の開始点です。

結果が心配です-「グローバルリスト」を更新するとおっしゃっています。一度に複数のスレッドがそのリストを同時に更新しようとすると、問題が発生する可能性があります。いくつかのオプション:

  1. リストが適切にスレッドセーフであることを確認してください。これは簡単かもしれませんし、簡単ではないかもしれません - 正確に何が変更されているかによります。
  2. を使用ExecutorServiceしますがinvokeAll()、一連の Callables を並行して実行し、それらがすべて完了するまで待機するメソッドを使用します。その後、すべての結果を調べて、一度に 1 つずつ更新できます。結果にスレッドの問題はありません。これは、コードが Runnable の代わりに Callable を実装する必要があることを意味します (大したことではありません)。 ここに例のあるブログがあります
于 2012-06-20T20:55:49.413 に答える
-1

さてサム...私はあなたの質問であまりクリアされていません.....

これを試してみてください...

以下は、uが複数のインスタンスを実行するのに役立つコードです......。

メインスレッド

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

        }         

If you are using functions that should be restricted to only one thread at a time...

include the syntax....

   public synchronized func_type func_name()
   {


   }
于 2012-06-20T21:11:29.613 に答える