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…
im lost on how to start..I have some code Ill try to post later
10 3073
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…
im lost on how to start..I have some code Ill try to post later
Till you post the code then we'll all keep our fingers crossed.
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…
im lost on how to start..I have some code Ill try to post later
Two remarks:
1) You can't pass anything by reference in Java; never; really.
2) Integers are immutable; you can't use them for your purposes.
No reason to panic though, you can write your own little class: -
public class MutableInteger {
-
private int i;
-
public MutableInteger() { this(0); }
-
public MutableInteger(int i) { this.i= i; }
-
-
// increment i, (un) synced:
-
public int inc() { return ++i; }
-
public synchronized syncInc() { return ++i; }
-
}
-
simply pass one of those MutableInteger objects to your threads and increment away.
kind regards,
Jos
interesting how that was part of the requirments...? I am currently at work. I will post code later in the day, probably after 3:30...Thanks
Two remarks:
1) You can't pass anything by reference in Java; never; really.
2) Integers are immutable; you can't use them for your purposes.
No reason to panic though, you can write your own little class: -
public class MutableInteger {
-
private int i;
-
public MutableInteger() { this(0); }
-
public MutableInteger(int i) { this.i= i; }
-
-
// increment i, (un) synced:
-
public int inc() { return ++i; }
-
public synchronized syncInc() { return ++i; }
-
}
-
simply pass one of those MutableInteger objects to your threads and increment away.
kind regards,
Jos
It could have been ... enterprising to see is code first.
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!
without synchronization? -
private Integer sum = new Integer(0);
-
Please help!
You simply ignored my remark?
kind regards,
Jos
Im sorry, i dont follow..what did I do? i was doing this the way our instructor wants...i just need help to make it work....no disrespect meant...purely an accident
My most recent working code
how would I implement sychronization 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);
-
}
-
}
-
}
I just wanted to check and see if anyone is out there today
I just wanted to check and see if anyone is out there today
I don't see why you're using an Integer instead of a simple int. But maybe that
is a (superfluous) requirement.
So what happens when you let all your threads increment that 'shared resource'
in an (un)synchronized way?
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: OJ |
last post by:
Hey,
I'm trying to make a right justified, bullet on the right, list. The
only way that I've found is to use the "direction" attribute:
<style>
ul.rtl {
direction: rtl;
}
</style>
<ul...
|
by: Graham Blandford |
last post by:
Hi All, wonder if anyone can help me with some direction...
I'm a VB6er who has been plunged into the VB.NET arena with a requirement to
complete a reasonably small project within a month..
The...
|
by: Milagro |
last post by:
Hi,
I'm a perl/C programmer but a Javascript problem has been dumped into my
lap. A client of mine has a freeware javascript on their site which shows a
series of pictures on a web page in a...
|
by: gary |
last post by:
Hi,
1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
|
by: Davide |
last post by:
Hi
We have a stored procedure for inserting data into a
table and another for Updating that table.
For the Insert SP we use a LastModifiedDateTime output
parameter and for the Update SP we...
|
by: junky_fellow |
last post by:
Hi all,
Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?
I know this question is...
|
by: dancingtaotaichi4u |
last post by:
How do I change the direction of text IN A TABLE when text direction is
not offered under Format?
|
by: Santosh |
last post by:
dear all i want to change text direction of text box with in datagrid
control i am writting following code but it is not working.
public void SetUpClientScript(object sender,...
|
by: veer |
last post by:
Hi
i want to move in a back direction in any string
just like substring function in which we can move from a particular location to another location in forward direction but i want to move backword...
|
by: RufusA |
last post by:
Is there any way of getting float statements, padding etc. to take
account of the directionality of a page (i.e. the direction:rtl )
For example I have an old simple web page that uses *shudder*...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |