why does this programs speed not increase? 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 &gt; 0);

        int array[] = new int [N];

        assert(array != null);

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

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

        Date start = new Date();

        int max = array[0];

        for (int i = 1; i &lt; N; i++) {
            if (array[i] &gt; 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 &lt; this.end; i++) {
            if (array[i] &gt; max) {
                max = array[i];
            }
        }


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

    }

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


        assert(array != null);

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

        for (int i = 0; i &lt; 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&lt;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?
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.