2017-09-05 156 views
-2

1)熱衷於我們運行方法1,2,3平行如何使用Java 8如何運行3種方法同時或並行使用Java 8流

2知道)這是不是正確的方式,以滿足我的要求用java 8使用流?

public void RunParallel() 
{ 
    String method1 = method1(); 
    String method2 = method2(); 
    String method3 = method3(String Value); 
} 

Stream.<Runnable>of(() -> method1(),() -> method2(),() -> method3()).parallel().forEach(Runnable::run);

+0

爲每個線程和調用start()他們。 – daniu

+0

目前我正在尋找它的java 8版本 – user3663894

+0

@daniu「創建線程」幾乎總是錯誤的答案。優先使用'Executor'來直接創建線程。 –

回答

1

你可以使用Java 8以下,用下面的代碼方法1,方法2並行&方法3運行。如果你想在所有完成之後做一些事情,那麼你可以使用future.get();

public void RunParallel() 
{ 
    CompletableFuture<Void> future1 = CompletableFuture.runAsync(()->{ 
     String method1 = method1(); 
    }); 

    CompletableFuture<Void> future2 = CompletableFuture.runAsync(()->{ 
     String method2 = method2(); 
    }); 

    CompletableFuture<Void> future3 = CompletableFuture.runAsync(()->{ 
     String method3 = method3("some inp"); 
    }); 

    CompletableFuture<Void> future = CompletableFuture.allOf(future1, future2, future3); 
    try { 
     future.get(); // this line waits for all to be completed 
    } catch (InterruptedException | ExecutionException e) { 
     // Handle 
    } 
} 
+0

目前方法不需要等待對方 – user3663894

+0

我已經更新了我的回答的描述,請檢查。通過我的解決方案,所有三種方法都可以並行運行。 – karthikdivi

0

爲要並行這樣運行的每個不同的方法擴展Thread類:

public static void main(String[] args) { 
    new Operation1().start(); 
    new Operation2().start(); 
    ... 
} 

public class Operation1 extends Thread{ 
    public Operation1() { 
     super("Operation1"); 
    } 
    @Override 
    public void run() { 
     method1(); 
    } 
} 
public class Operation2 extends Thread{ 
... 
} 
+1

而不是創建線程類的,我覺得它更簡單通過給方法來構造,如在'新主題(這::方法1)。開始()創建線程;'。 – daniu

+0

正在尋找它的Java 8版本 – user3663894

+0

@daniu's是一個Java8版本。 '::'版本與我的差不多,但更緊湊。 Daniu的變體只是讓編譯器生成接口。 Tahts得多,當然更短,但只有有用的一個方法 – Marvin

0

很簡單並行

Thread thread1=new Thread() { 
    public void run() { 
     method1(); 
    } 
}; 
thread1.start(); 

重複運行的方法,這對所有的方式你剩下的方法。

下面是你的示例代碼。這裏所有三個線程並行執行相應的方法。

public class ThreadTest1 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Thread thread1 = new Thread() { 
      public void run() { 
       method1(); 
      } 
     }; 
     thread1.start(); 

     Thread thread2 = new Thread() { 
      public void run() { 
       method2(); 
      } 
     }; 
     thread2.start(); 

     Thread thread3 = new Thread() { 
      public void run() { 
       method3(); 
      } 
     }; 
     thread3.start(); 

    } 

    static void method1() { 

     try { 
      while(true){ 
      System.out.println("test1"); 
      Thread.currentThread().sleep(1000); 
      } 
     } catch (InterruptedException e) { 
     } 
    } 

    static void method2() { 
     try { 
      while(true){ 
      System.out.println("test2"); 
      Thread.currentThread().sleep(1000); 
      } 
     } catch (InterruptedException e) { 
     } 
    } 

    static void method3() { 
     try { 
      while(true){ 
      System.out.println("test3"); 
      Thread.currentThread().sleep(1000); 
      } 
     } catch (InterruptedException e) { 
     } 
    } 

} 
+0

使用java 8的東西? – user3663894

+0

我會堅持使用java基礎知識。也許有些東西會在java8中。不確定。但我希望它保持簡單。 – nagendra547

相關問題