Scenerio:
- I have an integer array of 15 indexes. (each populated with some value)
- I want two threads to add two (+2) to the value of the index. Each being aware which index has already been added.
- Then join the threads and exit.
So far (to where I made it) I'm simply implementing the Runnable interface and supplying the array to computed inside of runnable. I'm stuck with how to do this handshaking from here. Thanks in advance for the help
public class FooRunnable implements Runnable
{
private int[] myArray = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
@Override
public void run()
{
for(int i=0; i<myArray.length; i++)
{
myArray[i] = myArray[i] +2;
System.out.println("Thread "+Thread.currentThread().getName()+" Finished index: "+i);
atomicCount.incrementAndGet();
}
}
public static void main(String[] args)
{
FooRunnable r = new FooRunnable();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.setName("Thread One");
t2.setName("Thread Two");
t1.start();
t2.start();
}
}
Results (Goal):
When program exits. the array should have each element of the array incremented by two via two different threads.
Threadits own partition to increment.