2011-03-24 3 views
0

병렬 프로그래밍을 사용하여 선형 방정식을 풀기 위해 java로 프로그램을 개발 중입니다. 나는 그것을 해결하기위한 코드를 작성했다. 멀티 코어 프로세서에서 작동하도록 수정하는 방법? 몇 가지 예를 들어 보겠습니다. mpi (메시지 전달 인터페이스)에 대해 들었습니다. 내 코드에서 어떻게 사용합니까?자바 프로그램을 병렬 처리하는 방법은 무엇입니까?

+0

http://download.oracle.com/javase/tutorial/essential/concurrency/ –

+2

첫 번째 단계는 프로그램을 작은 조각으로 분리 할 수있는 방법을 결정합니다. 다음 단계는 실제로 작은 조각들을 병렬 처리하는 것이 더 빠르다는 것을 결정하는 것입니다. – Jeremy

+0

병렬 프로그램을 작성하기 위해 java를 프로그래밍 언어로 사용해야합니까? – Bartzilla

답변

4

를 참조하십시오, 당신은 튜토리얼 here을 볼 수 있습니다.

예는 수 :

import java.util.concurrent.*; 
import java.util.*; 

class MyThreadPoolExecutor 
{ 
    int poolSize = 2; 

    int maxPoolSize = 2; 

    long keepAliveTime = 10; 

    ThreadPoolExecutor threadPool = null; 

    final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
      5); 

    public MyThreadPoolExecutor() 
    { 
     threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, 
       keepAliveTime, TimeUnit.SECONDS, queue); 

    } 

    public void runTask(Runnable task) 
    { 
     // System.out.println("Task count.."+threadPool.getTaskCount()); 
     // System.out.println("Queue Size before assigning the 
     // task.."+queue.size()); 
     threadPool.execute(task); 
     // System.out.println("Queue Size after assigning the 
     // task.."+queue.size()); 
     // System.out.println("Pool Size after assigning the 
     // task.."+threadPool.getActiveCount()); 
     // System.out.println("Task count.."+threadPool.getTaskCount()); 
     System.out.println("Task count.." + queue.size()); 

    } 

    public void shutDown() 
    { 
     threadPool.shutdown(); 
    } 

    public static void main(String args[]) 
    { 
     MyThreadPoolExecutor mtpe = new MyThreadPoolExecutor(); 
     // start first one 
     mtpe.runTask(new Runnable() 
     { 
      public void run() 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        try 
        { 
         System.out.println("First Task"); 
         Thread.sleep(1000); 
        } catch (InterruptedException ie) 
        { 
        } 
       } 
      } 
     }); 
     // start second one 
     /* 
     * try{ Thread.sleep(500); }catch(InterruptedException 
     * ie){} 
     */ 
     mtpe.runTask(new Runnable() 
     { 
      public void run() 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        try 
        { 
         System.out.println("Second Task"); 
         Thread.sleep(1000); 
        } catch (InterruptedException ie) 
        { 
        } 
       } 
      } 
     }); 
     // start third one 
     /* 
     * try{ Thread.sleep(500); }catch(InterruptedException 
     * ie){} 
     */ 
     mtpe.runTask(new Runnable() 
     { 
      public void run() 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        try 
        { 
         System.out.println("Third Task"); 
         Thread.sleep(1000); 
        } catch (InterruptedException ie) 
        { 
        } 
       } 
      } 
     }); 
     // start fourth one 
     /* 
     * try{ Thread.sleep(500); }catch(InterruptedException 
     * ie){} 
     */ 
     mtpe.runTask(new Runnable() 
     { 
      public void run() 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        try 
        { 
         System.out.println("Fourth Task"); 
         Thread.sleep(1000); 
        } catch (InterruptedException ie) 
        { 
        } 
       } 
      } 
     }); 
     // start fifth one 
     /* 
     * try{ Thread.sleep(500); }catch(InterruptedException 
     * ie){} 
     */ 
     mtpe.runTask(new Runnable() 
     { 
      public void run() 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        try 
        { 
         System.out.println("Fifth Task"); 
         Thread.sleep(1000); 
        } catch (InterruptedException ie) 
        { 
        } 
       } 
      } 
     }); 
     // start Sixth one 
     /* 
     * try{ Thread.sleep(500); }catch(InterruptedException 
     * ie){} 
     */ 
     mtpe.runTask(new Runnable() 
     { 
      public void run() 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        try 
        { 
         System.out.println("Sixth Task"); 
         Thread.sleep(1000); 
        } catch (InterruptedException ie) 
        { 
        } 
       } 
      } 
     }); 
      mtpe.shutDown(); 
    } 

} 
+0

가치있는 예제는 http://programmingexamples.wikidot.com/threadpoolexecutor에서 가져 왔습니다. – amit

관련 문제