Re: Multithreading / Scalability
- From: blmblm@xxxxxxxxxxxxx
- Date: 6 Feb 2006 09:43:11 GMT
In article <CEBFf.1864$cm2.572@xxxxxxxxxxxx>,
Knute Johnson <nospam@xxxxxxxxxxxxxxxxx> wrote:
[ snip ]
I wrote a
couple of tests of my own to try and got more predictable results.
Although not quite.
The first program below times the calculations as yours did but I think
it is still not optimum for this test.
The second program calculates the number of calculation cycles per ms
which I think is more to the point. I got very similar results to the
first test though and that is that one thread is definitely slower than
two threads but more than that don't really improve performance. On my
single processor machine, every increase in threads reduced the number
of calculations that could be performed although again not as
dramatically as I expected with the increase in number of threads.
[ snip ]
Interesting second test program (test3 below). Some questions, though:
Why do you have this?
Thread.sleep(2000); // wait till all threads are created
This seems like an ugly hack to avoid writing proper code to wait
until the threads are created? ugly hacks are not terrible in
quick-and-dirty code, but still.
Also, "calculations" is shared among threads, but you're not ensuring
one-at-a-time access with "synchronized" or some other mechanism.
Why do you think this will work? You do declare it "volatile",
but as I understand it, this only ensures atomic loads and stores,
while you also have a "++calculations". I would have said this
was not guaranteed to be atomic on all processors. No?
import java.util.concurrent.*;
public class test3 implements Runnable {
volatile long calculations;
volatile boolean runFlag = true;
Object o = new Object();
Semaphore sem;
public test3(String[] args) {
int numberOfThreads = Integer.parseInt(args[0]);
Thread[] thread = new Thread[numberOfThreads];
sem = new Semaphore(numberOfThreads);
try {
sem.acquire(numberOfThreads);
for (int i=0; i<numberOfThreads; i++) {
thread[i] = new Thread(this);
thread[i].start();
}
Thread.sleep(2000); // wait till all threads are created
long then = System.currentTimeMillis();
sem.release(numberOfThreads);
Thread.sleep(20000);
runFlag = false;
long now = System.currentTimeMillis();
System.out.println(calculations/(now-then));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public void run() {
try {
sem.acquire();
while (runFlag) {
double d = Math.sqrt(1234.56789);
double t = Math.tan(d);
++calculations;
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public static void main(String[] args) {
new test3(args);
}
}
--
Knute Johnson
email s/nospam/knute/
--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.
.
- Follow-Ups:
- Re: Multithreading / Scalability
- From: Knute Johnson
- Re: Multithreading / Scalability
- References:
- Multithreading / Scalability
- From: Philipp Kayser
- Re: Multithreading / Scalability
- From: Knute Johnson
- Multithreading / Scalability
- Prev by Date: Re: copying a graphics object
- Next by Date: Struts "bean:define" tag
- Previous by thread: Re: Multithreading / Scalability
- Next by thread: Re: Multithreading / Scalability
- Index(es):
Relevant Pages
|