A question about array sorting with multiple threads...

Hi,

i am trying to sort an integer containing array with multiple threads.

Problem is:

When i use one thread to sort an array of 5000000 integers, it takes around 1700 ms.

When i used two threads and give each of them an array of 2500000 integers, it takes around 1700 ms. again.

i tryed several times and same results everytime. I'm using a normal pc with one cpu.

is this logical?

thnx.

publicclass Test{

/**

* @param args

*/

staticint SIZE = 2500000;

staticint MAX = 1000000;

staticint MIN = 1;

publicstaticvoid main(String[] args)throws InterruptedException{

int[] a1 =newint[SIZE];

int[] a2 =newint[SIZE];

Random rnd =new Random();

//System.out.print("Array1:");

for(int i=0;i<SIZE ; i++){

//Returns a uniformly distributed int value between MIN (inclusive) and the specified value (exclusive)

a1[i] = rnd.nextInt(MAX-MIN+1) + MIN;

a2[i] = rnd.nextInt(MAX-MIN+1) + MIN;

//print genereated random numbers

//System.out.print(a1[i] + " ");

}

long x1 = System.currentTimeMillis();

SorterThread t1 =new SorterThread("1.",a1);

SorterThread t2 =new SorterThread("2.",a2);

t1.start();

t2.start();

t1.join();

t2.join();

long x2 = System.currentTimeMillis();

System.out.println();

System.out.println("elapsed time:" + (x2-x1));

}

}

>

[2706 byte] By [nullPointerXa] at [2007-11-27 3:41:40]
# 1

Its not called a thread of execution because it sounds good.

Each thread needs a CPU to execute. One CPU, one thread at a time.

Using two threads should take longer. The O/S needs to switch between threads so that each thread gets a fair share of CPU time. This switching time (context switching) eats up CPU time as well since the O/S needs the CPU itself.

You need to read some articles about how computers work or enroll in a CS course.

cooper6a at 2007-7-12 8:45:12 > top of Java-index,Core,Core APIs...
# 2
thanks for your very quick response.i actually taking a course about parallel algoritms for my ms.i thought soo too.but i also want to learn your opinionsthnx.
nullPointerXa at 2007-7-12 8:45:12 > top of Java-index,Core,Core APIs...