how to parallelize a java program? [closed]
am developing a program in java to solve linear equations using parallel programming. I've written the code for solving it. How to modify it to work on multicore processor? give me few examples. i heard of mpi (message passing interface). how to use it in my code?
The simplest way is to use ThreadPoolExecutors, you can see the Tutorial here.
An example could be:
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();
}
}
you can't the only thing you can do is multi threading and hope that the OS Supervisor will share your processes over multiple cores.
See http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html
精彩评论