2017-09-05 158 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

相关问题