2017-02-22 3 views
-4

Javaプログラムで推奨アルゴリズムを実装します。Javaで入力値を使って並列プログラミングを行う方法

しかし、重大な問題があります。データセットが大きすぎ、計算が遅すぎます。だから、私はJavaで並列プログラミングを行う必要があります。例えば

、私は、このような

process 1: for (int i=0; i < 10000 ; i++) 

process 2: for (int i=10001; i < 20000 ; i++) 

process 3: for (int i=20001; i < 30000 ; i++) 

として、この文章を分割したい

for (int i=0; i < 10000000 ; i++) { ~~~ } 

...

私は同様の方法in Pythonを知っています。 Javaで並列プログラミングを行うには?

+3

hereからそれを得ましたか。人々が並列にコードを書くときの並列プログラミングではないのですか?ちょうどペアプログラミングのようですが、パラレル! Javaスレッドを使用するだけです。 –

+0

Java 8の並列ストリームの使用を検討することもできます。 –

答えて

0

希望すると、これが役立ちます。

public class MyRunnable implements Runnable { 
     private final long countUntil; 

     MyRunnable(long countUntil) { 
       this.countUntil = countUntil; 
     } 

     @Override 
     public void run() { 
       long sum = 0; 
       for (long i = 1; i < countUntil; i++) { 
         sum += i; 
       } 
       System.out.println(sum); 
     } 
} 



public class Main { 

     public static void main(String[] args) { 
       // We will store the threads so that we can check if they are done 
       List<Thread> threads = new ArrayList<Thread>(); 
       // We will create 500 threads 
       for (int i = 0; i < 500; i++) { 
         Runnable task = new MyRunnable(10000000L + i); 
         Thread worker = new Thread(task); 
         // We can set the name of the thread 
         worker.setName(String.valueOf(i)); 
         // Start the thread, never call method run() direct 
         worker.start(); 
         // Remember the thread for later usage 
         threads.add(worker); 
       } 
       int running = 0; 
       do { 
         running = 0; 
         for (Thread thread : threads) { 
           if (thread.isAlive()) { 
             running++; 
           } 
         } 
         System.out.println("We have " + running + " running threads. "); 
       } while (running > 0); 

     } 
} 

私はあなたがスレッドを意味

関連する問題