2017-01-04 5 views
0

マルチスレッドを学習するために、Executor Frameworkを使用して2つの異なるタスクを実行するための実装プログラムを作成しました。以前は、同期メソッドを使用してこの要件を満たしていましたが、誤った結果が出ました。次に、Executor Frameworkを使用する方がスレッド管理の方が優れていることを知りました。 Progam以下エグゼキュータフレームワークを使用した複数のタスク

方法

import java.io.*; 
import java.util.Scanner; 
import java.nio.*; 
class FileWriteThreadExample implements Runnable{ 
    /*This class needs to write some content into text file*/ 

    public synchronized void run() { 
      StringBuilder thisProgamMessage = new StringBuilder(); 

      try(FileWriter fw = new FileWriter("C:\\TestNotes.txt", true); 
       BufferedWriter bw = new BufferedWriter(fw); 
       PrintWriter out = new PrintWriter(bw)) 
      { 
       for(int i=1; i<=50;i++){ 
        //Thread.sleep(500); 
        //System.out.println(i); 

        thisProgamMessage.append(i+":"+Math.random()+"\n"); 

       } 
       out.println(thisProgamMessage.toString()); 
      } catch (IOException e) { 
       //exception handling left as an exercise for the reader 
      } 

    } 
} 

class FileWriteThreadExample2 implements Runnable{ 
    /*This class needs to write some content into text file*/ 

    public synchronized void run() { 
      StringBuilder thisProgamMessage = new StringBuilder(); 
      try(FileWriter fw = new FileWriter("C:\\TestNotes.txt", true); 
       BufferedWriter bw = new BufferedWriter(fw); 
       PrintWriter out = new PrintWriter(bw)) 
      { 


       System.out.println("Starting Second Write Thread Task"); 
       for(int i=50; i>=1;i--){ 
        //Thread.sleep(500); 
        //System.out.println(i); 
        thisProgamMessage.append(i+"====>"+Math.random()+"\n"); 
       } 
       out.println(thisProgamMessage.toString()); 
       System.out.println("Completing Second Write Thread Task"); 
      } 
      catch (FileNotFoundException fnfe){ 
       fnfe.printStackTrace(); 
      } 
      catch(IOException ioex) { 
       ioex.printStackTrace(); 
      } 
      /*catch(InterruptedException ie){ 
       ie.printStackTrace(); 
      }*/  
    } 
} 
class SynchronizeTest { 
     public static void main (String[] args) { 
      FileWriteThreadExample t1 = new FileWriteThreadExample(); 
      FileWriteThreadExample2 t2 = new FileWriteThreadExample2(); 

      t1.start(); 

      t2.start(); 

     } 
    } 

を同期使用してここでの問題は、私は2つのタスクを実行するExecutorのためのコードを記述するのか分からないです。私は、すなわち

つのタスクを実行するための
ExecutorService es = Executors.newFixedThreadPool(5); 
    public void doStuff() { 


     es.submit(new MyRunnable()); 


    } 

をExecutorServiceのでコードを実装していた最後に、誰かがExecutorのフレームワークを持つ2つの異なるタスクを実装するために私を提案することができますか?

PS:私は運動のあなたの意図を知らない、問題文の

答えて

1

あなたは非常に近いです:

ExecutorService es = Executors.newFixedThreadPool(5); 
public void doStuff() { 
    es.submit(new FirstTask()); // FirstTask implements Runnable 
    es.submit(new SecondTask()); // SecondTask implements Runnable 
} 

または代わりに:あなたは、生のスレッドの中で自分自身をタスクを実行していたかのように

ExecutorService es = Executors.newFixedThreadPool(5); 
public void doStuff() { 
    Collection<Runnable> tasks = Arrays.asList(new Runnable[] 
      { new FirstTask(), new SecondTask() }); 
    es.invokeAll(tasks); 
} 

各タスクは、通常のように相互に同期することができます。

+0

トラヴィス、ありがとうございました。 – Ankit

0

を理解する上の任意の混乱のために私を知ってみましょう。あなたの同期バージョンで。あなたは何も同期しません。 2つのスレッドはTestNotes.txtに順番にアクセスします。なぜなら、1つのファイルだけが書き込み用にファイルを開くことができるからです。これはあなたの意図ですか?

+0

私の使用例では、2つのスレッドが1つのテキストファイルに異なるテキストを書きたいと考えています。したがって、executorフレームワークを使用する必要があります。 – Ankit

関連する問題