This program assumes multi-core CPU and split the process and execute them parallelly.
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
public class parallel {
int x[] = new int[1000000];
float xcl[] = new float[1000000];
float xcl2[] = new float[1000000];
float y2[];
void process() throws InterruptedException, BrokenBarrierException{
//Instantiate values
for(int i =0; i<1000000;i++){
xcl[i] = i;
}
//Create the executor object
ExecutorService exec = Executors.newCachedThreadPool();
//Create of CPU tasks
class AddTask extends Thread {
public int n = 0;
AddTask(int x){
// this.setPriority(10);
n = x;
}
public void run() {
for(int i =n; i< n + 500000 -1;i++){
for(int j = 0 ; j<10000;j++){
x[i] = x[i]+ j;
}
}
//Barrier to control thresds
}
}
Runnable t1 = new AddTask(0);
Runnable t2 = new AddTask(500000);
exec.submit(t2);
exec.submit(t1);
exec.shutdown();
}
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
(new parallel()).process();
}
}
No comments:
Post a Comment