Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is the program I was given, it searches a large array for a max value:

import java.util.Date;
import java.util.Random;

class FindMax {
    private static final int N = 256 * 1024 * 1024;

    public static void main(String args[]) {
        assert(N > 0);

        int array[] = new int [N];

        assert(array != null);

        Random random = new Random(new Date().getTime());

        for (int i = 0; i < N; i++) {
            array[i] = random.nextInt();
        }

        Date start = new Date();

        int max = array[0];

        for (int i = 1; i < N; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }

        Date end = new Date();

        System.out.println("max: " + max);
        System.out.println("time in msec: " + (end.getTime() - start.getTime()));
    }
}

I am to increase the speed by having multiple threads each search part of the array. This is what i did:

import java.util.Date;
import java.util.Random;

class FindMax extends Thread{
    private static final int N = 256 * 1024 * 1024;

    static int array[] = new int [N];
    static int max = array[0];
    int start, end;

    public void run(){

        for (int i = this.start; i < this.end; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }


    }
    public FindMax(int q, int u){
        this.start=q;
        this.end=u;

    }

    public static void main(String args[]) {
        assert(N > 0);
        int ts = Integer.parseInt(args[0]);


        assert(array != null);

        Random random = new Random(new Date().getTime());

        for (int i = 0; i < N; i++) {
            array[i] = random.nextInt();
        }

        Date start = new Date();

        Thread t[] = new Thread[ts];
        int eacht = (256 * 1024 * 1024)/ts;



        for( int p=0; p<ts;p++){
            t[p] = new FindMax(0+p*eacht, (eacht-1)+p*eacht);
            t[p].start();
        }





        Date end = new Date();

        System.out.println("max: " + max);
        System.out.println("time in msec: " + (end.getTime() - start.getTime()));
    }
}

The problem is that is has the opposite effect. The more threads (taken from command line argument) the LONGER the program takes to run. Why is this doing the exact opposite of increasing the programs speed with increased threads created?

share|improve this question
1  
Why should it increase speed? Unless the threads are run on different processors that is? –  Hovercraft Full Of Eels Feb 25 '12 at 22:23
    
How many cores does your processor have? –  smessing Feb 25 '12 at 22:25
5  
don't you have to wait for the threads to complete? you are timing the how long it takes to start the threads not complete the work. –  mike z Feb 25 '12 at 22:26
    
The threaded solution you present may not even produce the correct answer, unless you add some synchronization. –  claesv Feb 25 '12 at 22:31
2  
All of your threads are writing to a single shared max field declared as static. This can give wrong results (because, for example, a thread might see that array[i] > max right before some other thread increases max, and then perform max = array[i] right afterward), and also can cause significant slowdown (because many processors writing to the same memory location will keep invalidating each other's caches of what's in that location). Instead, you need each thread to write to its own max, and then, once the threads finish, you can go through and find the greatest max. –  ruakh Feb 25 '12 at 22:33
show 1 more comment

migrated from stackoverflow.com Feb 26 '12 at 5:32

This question came from our site for professional and enthusiast programmers.

1 Answer

Contrary to your belief, running a program with more threads than the number of processors (Runtime.getRuntime().availableProcessors()) will not increase the performance of your program, in fact it will decrease performance. The decrease in performance occurs because your processor has to jump from thread to thread. That is not a time-free operation.

The exception to this rule being if you have a lot of blocking I/O operations in your threads, in that case, more threads than the number of processors may be appropriate for your use.

I suggest you use an ExecutorService which runs with the correct number of threads (possibly obtained through one of the factory methods in Executors) and submit tasks for it to complete.

share|improve this answer
8  
+1: Even running a number of threads equal to or less than the number of processors may not increase performance, as creating the threads and coordinating them introduces a lot of overhead. –  Ernest Friedman-Hill Feb 25 '12 at 22:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.