第一种
public class SleepSortOne implements Runnable {
int number;
public static int length = 0;
public SleepSortOne(int number) {
this.number = number;
}
public void run() {
try {
Thread.sleep(number);
System.out.println("排序结果:"+number/length);
} catch (Exception e) {
}
}
public static void main(String[] args) {
int numbers[] = {3, 3434, 3435, 3436, 1236, 1, 5555, 1235, 3, 3434, 3435, 3436, 1236, 1, 5555, 1235};
length = numbers.length;
for (int i = 0; i < length ; i++) {
// 增加睡眠的间隔时间,防止因为时间过短导致的睡眠不足问题(针对间隔较小的数字排序)
new Thread(new SleepSortOne(numbers[i]*length)).start();
}
}
}
第二种
class SortThread extends Thread{
int ms=0;
public SortThread(int ms) {
this.ms=ms;
}
public void run() {
try {
sleep(ms*10+10);;
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(ms);
}
}
public static void main(String[] args) {
int[] ints = {1,4,7,3,8,9,2,6,5};
SortThread[] sortThreads=new SortThread[ints.length];
for(int i=0;i<sortThreads.length;i++) {
sortThreads[i]=new SortThread(ints[i]);
}
for(int i=0;i<sortThreads.length;i++) {
sortThreads[i].start();
}
}