我正在开发一个java程序来解决使用并行编程的线性方程。我已经编写了解决它的代码。如何修改它以在多核处理器上工作?给我几个例子。我听说过mpi(消息传递接口)。如何在我的代码中使用它?如何并行化一个java程序?
0
A
回答
0
你不可能做的唯一事情就是多线程,并希望OS Supervisor将在多个内核上共享你的进程。
见http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html
4
最简单的方法是使用ThreadPoolExecutors,你可以看到教程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 2011-03-24 13:56:47
http://download.oracle.com/javase/tutorial/essential/concurrency/ – 2011-03-24 13:54:18
的第一步是决定该程序可以如何被分解成更小的碎片。下一步是决定是否有意义(更快)并行化这些小块。 – Jeremy 2011-03-24 13:54:47
你是否需要使用java作为编程语言来编写你的并行程序? – Bartzilla 2011-03-24 14:02:04