General discussion

Locked

Parallelism and Concurrency in java

By shaudy_911 ·
Write a parallel program in java to add the elements of an array of 20 integers.

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Parallelism and Concurrency in java

by roland11 In reply to Parallelism and Concurren ...

// loads an Array in parallel Threads

/**
* @author Roland Schmidt
*/
public class ArrayLoader {
public void load(final int[] array) {
Thread[] threadArr = new Thread[array.length];
// create Threads
for (int i=0; i<array.length; ++i) {
final int ix = i;
threadArr = new Thread() {
public void run() {
array[ix] = ix;
}
};
}
// start
for (int i=0; i<threadArr.length; ++i) {
threadArr.start(); }
// wait for completion
for (int i=0; i<threadArr.length; ++i) {
try {
threadArr.join();
} catch (InterruptedException e) {
}
}
}


public static void main(String args[]) {
int[] intArray = new int[20];
for (int i=0; i<intArray.length; ++i) System.out.print(" "+intArray);
System.out.println();
new ArrayLoader().load(intArray);
for (int i=0; i<intArray.length; ++i) System.out.print(" "+intArray);
System.out.println();
}

}

// Greetings
// Roland

Collapse -

by shaudy_911 In reply to Parallelism and Concurren ...

The question was auto-closed by TechRepublic

Collapse -

by shaudy_911 In reply to Parallelism and Concurren ...

The question was auto-closed by TechRepublic

Collapse -

by shaudy_911 In reply to Parallelism and Concurren ...

This question was auto-closed because it is 90 days old

Collapse -

by shaudy_911 In reply to Parallelism and Concurren ...

This question was auto-closed because it is 90 days old

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums