Help Please

I need to write a program that launches 1000 threads. Each thread adds 1 to a variable sum that is initially 0. I need to pass sum by reference to each thread. In order to pass by reference, define an Integer wrapper object to hold the sum. Run the program with and without synchronization to see the effects.

Ex output:

At thread 1 sum = 0;

At thread 2 sum = 1,

Etc

i am clueless

[417 byte] By [dlangdamana] at [2007-11-27 10:13:41]
«« hi friends
»» error
# 1

<delete>

Message was edited by:

sabre150

sabre150a at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 2

> I need to pass sum by reference to each thread.

Interesting!

aniseeda at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 3

how so? im not sure why that is a requirement I have been getting the feeling it cant be done....any help?

dlangdamana at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 4

What happened Sabre 150?

dlangdamana at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 5

Why do you think it can't be done?

It is quite easy.

First you create an Integer object with a value of 0 and assign it to a reference variable.

Create a thread class that can see the reference variable then add 1 to its value.

In your main method create a for loop that will create 1000 thread objects and start each of them after creating them.

With this you should be able to get started and let us know if you run into problems. We don't do homework here, but if you make a concerted effort and ask a SPECIFIC question we can be more help.

maple_shafta at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 6

> What happened Sabre 150?

I forgot to take the little pills today.

sabre150a at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 7

without synchronization?

import java.util.concurrent.*;

public class Threads {

private Integer sum = new Integer(0);

public static void main(String[]args) {

Threads test = new Threads();

System.out.println("What is Sum ? " + test.sum);

}

public Threads() {

ExecutorService executor = Executors.newFixedThreadPool(1000);

//Not sure right here?

executor.shutdown();

while(!executor.isTerminated()){

}

}

//without synchronization

class SumTask implements Runnable {

public void run(){

//not sure here?

}

}

}

no idea where to go from there

with syncronization

import java.util.Concurrent.*;

public class Threads2 {

private Integer sum = new Integer(0);

public synchronized static void main(String[]args) {

Threads2 test = new Threads2();

}

public Threads2(){

ExecutorService executor = Executors.newFixedThreadPool(1000);

// dont know

}

executor.shutdown();

while(!executor.isTerminated()){

}

// with sync

class SumTask2 implements Runnable {

public synchronized void run(){

// dont know

}

}

}

Please help!

dlangdamana at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 8

Most recent working code....how do i do the synchronization with and without?

import java.util.concurrent.*;

public class Thread {

private Integer sum = new Integer(0);

public static void main(String[] args) {

Thread test = new Thread();

System.out.println("What is sum ? " + test.sum);

}

public Thread() {

ExecutorService executor = Executors.newFixedThreadPool(1000);

for (int i = 0; i < 1000; i++) {

executor.execute(new SumTask());

}

executor.shutdown();

while(!executor.isTerminated()) {

}

}

class SumTask implements Runnable {

public void run() {

int value = sum.intValue() + 1;

sum = new Integer(value);

}

}

}

dlangdamana at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 9

anyone out there

dlangdamana at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 10

wow...noone was around on this one...Thanks Guys

dlangdamana at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 11

> wow...noone was around on this one...Thanks Guys

Why the sarcastic "Thanks Guys"? You realize that such remarks won't result in a constructive reply, don't you?

prometheuzza at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 12

> wow...noone was around on this one...Thanks Guys

double my pay, and I'll consider looking at your problem.

petes1234a at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 13

I suggest peppering your code with System.out.println() statements to see if every step works as you expect. Its crude, but effective.

George123a at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 14

> I need to pass sum by reference

Impossible in Java.

jverda at 2007-7-28 15:28:26 > top of Java-index,Java Essentials,Java Programming...
# 15

> Why do you think it can't be done?

>

> It is quite easy.

>

> First you create an Integer object with a value of 0

> and assign it to a reference variable.

>

> Create a thread class that can see the reference

> variable then add 1 to its value.

Still can't pass anything by reference though.

jverda at 2007-7-28 15:28:32 > top of Java-index,Java Essentials,Java Programming...
# 16

> Ex output:

>

> At thread 1 sum = 0;

> At thread 2 sum = 1,

Is it required that T1 gives 0, T2 gives 1, and so on, with specific threads tied to specific values? If so, this is a horrendously stupid exercise, and the task should be done without any multithreading at all, since you're completely defeating the purpose of multithreading and turning it into sequential code.

If it doesn't matter if T52 increments to 1 and then T888 increments to 2, and then T7 increments to 3, then it makes a little more sense.

jverda at 2007-7-28 15:28:32 > top of Java-index,Java Essentials,Java Programming...