473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

any direction would be gr8

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
Jul 11 '07 #1
10 3073
r035198x
13,262 8TB
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.
Jul 11 '07 #2
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. public class MutableInteger {
  2.    private int i;
  3.    public MutableInteger() { this(0); }
  4.    public MutableInteger(int i) { this.i= i; }
  5.  
  6.    // increment i, (un) synced:
  7.    public int inc() { return ++i; }
  8.    public synchronized syncInc() { return ++i; }
  9. }
  10.  
simply pass one of those MutableInteger objects to your threads and increment away.

kind regards,

Jos
Jul 11 '07 #3
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
Jul 11 '07 #4
r035198x
13,262 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. public class MutableInteger {
  2.    private int i;
  3.    public MutableInteger() { this(0); }
  4.    public MutableInteger(int i) { this.i= i; }
  5.  
  6.    // increment i, (un) synced:
  7.    public int inc() { return ++i; }
  8.    public synchronized syncInc() { return ++i; }
  9. }
  10.  
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.
Jul 11 '07 #5
without synchronization?

Expand|Select|Wrap|Line Numbers
  1. import java.util.concurrent.*;
  2.  
  3. public class Threads {
  4.     private Integer sum = new Integer(0);
  5.  
  6.     public static void main(String[]args) {
  7.         Threads test = new Threads();
  8.         System.out.println("What is Sum ? " + test.sum);
  9.     }
  10.     public Threads() {
  11.         ExecutorService executor = Executors.newFixedThreadPool(1000);
  12.         //Not sure right here?
  13.           executor.shutdown();
  14.  
  15.           while(!executor.isTerminated()){
  16.  
  17.           }
  18.     }
  19.     //without synchronization
  20.     class SumTask implements Runnable {
  21.         public void run(){
  22.             //not sure here?
  23.         }
  24.     }
  25. }
no idea where to go from there

with syncronization

Expand|Select|Wrap|Line Numbers
  1. import java.util.Concurrent.*;
  2.  
  3. public class Threads2 {
  4.     private Integer sum = new Integer(0);
  5.  
  6.     public synchronized static void main(String[]args) {
  7.         Threads2 test = new Threads2();
  8.     }
  9.  
  10.     public Threads2(){
  11.         ExecutorService executor = Executors.newFixedThreadPool(1000);
  12.         // dont know
  13.     }
  14.  
  15.     executor.shutdown();
  16.  
  17.     while(!executor.isTerminated()){
  18.     }
  19.     // with sync
  20.     class SumTask2 implements Runnable {
  21.         public synchronized void run(){
  22.             // dont know
  23.         }
  24.     }
  25. }
Please help!
Jul 11 '07 #6
JosAH
11,448 Expert 8TB
without synchronization?
Expand|Select|Wrap|Line Numbers
  1.     private Integer sum = new Integer(0);
  2.  
Please help!
You simply ignored my remark?

kind regards,

Jos
Jul 11 '07 #7
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
Jul 11 '07 #8
My most recent working code

how would I implement sychronization and without
Expand|Select|Wrap|Line Numbers
  1. import java.util.concurrent.*;
  2.  
  3. public class Thread {
  4.   private Integer sum = new Integer(0);
  5.  
  6.   public static void main(String[] args) {
  7.     Thread test = new Thread();
  8.     System.out.println("What is sum ? " + test.sum);
  9.   }
  10.  
  11.   public Thread() {
  12.     ExecutorService executor = Executors.newFixedThreadPool(1000);
  13.  
  14.     for (int i = 0; i < 1000; i++) {
  15.       executor.execute(new SumTask());
  16.     }
  17.  
  18.     executor.shutdown();
  19.  
  20.     while(!executor.isTerminated()) {
  21.     }
  22.   }
  23.  
  24.   class SumTask implements Runnable {
  25.     public void run() {
  26.       int value = sum.intValue() + 1;
  27.       sum = new Integer(value);
  28.     }
  29.   }
  30. }
Jul 11 '07 #9
I just wanted to check and see if anyone is out there today
Jul 12 '07 #10
JosAH
11,448 Expert 8TB
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
Jul 12 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
5
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...
5
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...
3
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...
0
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...
18
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...
0
by: dancingtaotaichi4u | last post by:
How do I change the direction of text IN A TABLE when text direction is not offered under Format?
1
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,...
4
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...
2
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*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.