Results 1 to 3 of 3
- 06-29-2010, 06:53 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 12
- Rep Power
- 0
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
And my sum methodJava Code:private volatile boolean running = true; private static Object mutex; private static ArrayList <Sums> sum = new ArrayList<Sums>(); private double 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.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); }
- 06-29-2010, 08:40 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
- 06-29-2010, 01:19 PM #3
Similar Threads
-
keep child threads running after parent thread dies
By adammyth in forum Threads and SynchronizationReplies: 2Last Post: 01-27-2010, 01:43 PM -
One socket and multiple threads
By DC200 in forum NetworkingReplies: 1Last Post: 12-30-2009, 06:32 AM -
multiple threads access the same method.
By bhanu in forum New To JavaReplies: 3Last Post: 02-16-2009, 06:54 AM -
threads question
By sandor in forum Threads and SynchronizationReplies: 9Last Post: 02-07-2009, 08:57 PM -
running multiple server
By amitnayak1 in forum Advanced JavaReplies: 3Last Post: 06-05-2008, 04:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks