Results 1 to 3 of 3
  1. #1
    ferdzz is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default Question about running Multiple Threads

    Hi all,

    I am working on an exercise that tells me:

    Assume that in the class that you are currently writing, there is already a method long calculate() that returns a long value produced by an algorithm that takes an unpredictable time to finish.
    Write a method void sum(int n) that launches a total of n new threads, each of which calls this method calculate, and after all these threads have finished, your method sum returns the sum of values that were produced by these n calls.

    I have written my code, I need is confirmation if the code is logically and syntactically correct. Thanks in advance.

    Here are my instance fields
    Java Code:
            private volatile boolean running = true;
    	private static Object mutex;
    	private static ArrayList <Sums> sum = new ArrayList<Sums>();
    	private double total;
    And my sum method
    Java Code:
    	
            public void sum(int n){
    		for (int i = 0; i < n; i++){
    			new Thread(new Runnable(){
    				public void run() {
    					try{
    						while(running){
    							synchronized(mutex){
    								this.wait();
    								for (Sums e: sum){
    									calculate();
    									total += e;
    								}		
    							}
    						}
    					}catch (InterruptedException e){}
    				}
    			}).start();
    		} System.out.print(total);
    	}
    There is also one syntax error, on the line "total += e;" and there were no suggestions on Eclipse on how to fix it. Any help is greatly appreciated.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ferdzz View Post
    There is also one syntax error, on the line "total += e;" and there were no suggestions on Eclipse on how to fix it. Any help is greatly appreciated.
    Variable 'e' has type Sums; you can only use the'+' operator on primitive numeric types (ints, doubles etc) and Strings (the latter is a little compiler trick).

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default

    Add a method to the Sums class to return its value as double.

Similar Threads

  1. keep child threads running after parent thread dies
    By adammyth in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 01:43 PM
  2. One socket and multiple threads
    By DC200 in forum Networking
    Replies: 1
    Last Post: 12-30-2009, 06:32 AM
  3. multiple threads access the same method.
    By bhanu in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 06:54 AM
  4. threads question
    By sandor in forum Threads and Synchronization
    Replies: 9
    Last Post: 02-07-2009, 08:57 PM
  5. running multiple server
    By amitnayak1 in forum Advanced Java
    Replies: 3
    Last Post: 06-05-2008, 04:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •