Test class:
[Java]
/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: Write a multi-threaded program.
* Author: Lei hengxin
* Completion date: January 1, November 20, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input Description: First encapsulate a class of object RandomNumber. The function is to first generate a random integer n greater than 10, then generate n random numbers and store them in the array.
* Input Description: encapsulate Thread1 (sub-class of Thread required) and Thread2 (Runnable interface required)
* Input Description: sorts the generated random number concurrently. Thread1 requires the Bubble sorting method and outputs the sorting result.
* Input Description: Thread2 requires sorting by fast sorting method and output the sorting result. Finally, write the main thread TestThread and add the above two threads to implement program concurrency,
* Input Description: Compare the sorting results of the two threads.
* Problem description:
* Program output:
* End the comment in the program Header
*/
Public class Test {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
System. out. println ("starting from the main thread ");
Thread1 thread1 = new Thread1 ();
Thread2 thread2 = new Thread2 ();
Thread t = new Thread (thread2 );
Thread1.start ();
T. start ();
Thread1.run ();
}
}
Thread1 class:
[Java]
Public class Thread1 extends Thread {
Public void run (){
RandomNumber randomNumber = new RandomNumber ();
RandomNumber. array ();
Int [] a = randomNumber. getA ();
Int t = 0;
For (int j = 0; j <a. length; j ++ ){
For (int I = 0; I <a. length-1-j; I ++ ){
If (a [I + 1]> a [I]) // if the next number is greater than the previous one
T = a [I];
A [I] = a [I + 1];
A [I + 1] = t;
}
}
System. out. println ("result of the fast Sorting Algorithm of thread 1 :");
For (int I = 0; I <a. length; I ++ ){
System. out. println (a [I]);
}
}
}
Thread2
Thread2 class:
[Java]
Public class Thread2 implements Runnable {
Private static void sort (int [] a, int first, int last)
{
Int I = first;
Int j = last;
Int middle = a [first]; // during the first call, I select the first number of arrays to be sorted as the key data.
While (true ){
While (++ I <last-1 & a [I] <middle)
;
While (-- j> first & a [j]> middle)
;
If (I> = j ){
Break;
}
Swap (a, I, j );
}
A [first] = a [j];
A [j] = middle;
If (first <j ){
Sort (a, first, j );
}
If (I <last ){
Sort (a, I, last );
}
}
Private static void swap (int [] a, int I, int j ){
Int temp = a [I];
A [I] = a [j];
A [j] = temp;
}
Public void run (){
System. out. println ("I am the header of thread 2 ");
RandomNumber randomNumber = new RandomNumber ();
RandomNumber. array ();
Int [] a = randomNumber. getA ();
Sort (a, 0, a. length );
System. out. println ("result of the fast Sorting Algorithm of thread 2 :");
For (int I = 0; I <a. length; I ++ ){
System. out. println (a [I]);
}
}
}
RandomNumber class:
[Java]
Public class RandomNumber {
// The function is to first generate a random integer n greater than 10, then generate n random numbers and place them in the array.
Int [];
Public int [] getA (){
Return;
}
Public void array (){
Int n = (int) (10 + 10 * Math. random ());
System. out. println ("the number of generated random numbers is" + n );
A = new int [n];
For (int I = 0; I <n; ++ I ){
A [I] = (int) (100 * Math. random ());
System. out. println (a [I]);
}
}
}
Running result: