Write a program that launches 100 threads. Each thread adds 1 to a variable sum that initially zero. You need to pass sum by reference to each thread. In order to pass it by reference, define an integer wrapper object to hold sum. Run the program with and without synchronization to see its effect.
code so far:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
//class that hold the variable SUM and add the value 1 into it.
class MutableInteger {
private int i;
//default constructor that sets the value to 0
public MutableInteger() {
this(0);
}
//constructor to pass and user defined value
public MutableInteger(int i) {
this.i= i;
}
public int inc() {
return ++i;
}
public synchronized int syncInc() {
return ++i;
}
}
public class mythread{
private static int countthread;
public static void main(String[] args){
/* Get an executor service that will run a maximum of 100 threads at a time: */
ExecutorService exec = Executors.newFixedThreadPool(100);
/* For all the 100 tasks to be done altogether... */
for ( countthread = 0; countthread <= 100; countthread++) {
/* ...execute the task to run concurrently as a runnable: */
exec.execute(new sum());
}
/* Tell the executor that after these 100 steps above, it will be done: */
exec.shutdown();
try {
/* The tasks are now running concurrently.wait until all work is done,
* with a timeout of 50 seconds: */
boolean b = exec.awaitTermination(50, TimeUnit.SECONDS);
/* If the execution timed out, false is returned: */
System.out.println("All done: " + b);
} catch (InterruptedException e) { e.printStackTrace(); }}
}
class sum extends Thread {
private MutableInteger sum = new MutableInteger();
public void run() {
/* do the work to be done in its own thread */
System.out.println("at " + Thread.currentThread()+"sum is "+sum.syncInc());
}
};
what i don't understand...
Im not sure wht does the question want by meaning this “Each thread adds 1 to a variable sum that initially zero”…
So does that mean the each thread will come out like this :
At thread 1 sum is 0
At thread 2 sum is 1
At thread 3 sum is 2
Or something like this:
At thread 1 sum is 1
At thread 2 sum is 1
At thread 3 sum is 1
Because each thread does add 1 to its value which is the variable sum where in the question itself will start as sum=0;
So meaning thread 1 will become 1, thread 2 will become 1 as shown here:
At thread 1 sum is 1
At thread 2 sum is 1
At thread 3 sum is 1
But, thread does uses other thread resources which is “at thread 2 sum is 1” but when thread 3 comes in it will uses the resources at thread 2 where the value itself have been already changed to the value 1 and when thread 3 comes it will add 1 to the value of variable sum so it should be “at thread 3 sum is 2” :
At thread 1 sum is 0
At thread 2 sum is 1
At thread 3 sum is 2
You guys please decide which is the correct output for the question???
And above code answer which I think the output is gonna be like this:
At thread 1 sum is 1
At thread 2 sum is 1
At thread 3 sum is 1