any direction would be gr8

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dlangdaman
    New Member
    • Jun 2007
    • 36

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Dlangdaman
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Dlangdaman
      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:

      [code=java]
      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; }
      }
      [/code]

      simply pass one of those MutableInteger objects to your threads and increment away.

      kind regards,

      Jos

      Comment

      • Dlangdaman
        New Member
        • Jun 2007
        • 36

        #4
        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

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          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:

          [code=java]
          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; }
          }
          [/code]

          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.
          Last edited by r035198x; Jul 11 '07, 11:49 AM. Reason: have have have have have have have have

          Comment

          • Dlangdaman
            New Member
            • Jun 2007
            • 36

            #6
            without synchronization ?

            Code:
            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

            Code:
            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!

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Dlangdaman
              without synchronization ?
              [code=java]
              private Integer sum = new Integer(0);
              [/CODE]
              Please help!
              You simply ignored my remark?

              kind regards,

              Jos

              Comment

              • Dlangdaman
                New Member
                • Jun 2007
                • 36

                #8
                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

                Comment

                • Dlangdaman
                  New Member
                  • Jun 2007
                  • 36

                  #9
                  My most recent working code

                  how would I implement sychronization and without
                  Code:
                  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);
                      }
                    }
                  }

                  Comment

                  • Dlangdaman
                    New Member
                    • Jun 2007
                    • 36

                    #10
                    I just wanted to check and see if anyone is out there today

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Dlangdaman
                      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)synchronize d way?

                      kind regards,

                      Jos

                      Comment

                      Working...