问题描述
package thread;import java.lang.Runnable;import java.math.*;import java.util.*;public class TestSort extends Thread{ int data[] = new int[10000]; String name; public TestSort(String name) { super(name); this.name = name; setTheArray(); } void setTheArray() { int i=0; while(i<10000) { data[i++]=(int)Math.random(); } } public void swap(int []array,int i,int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public void selectionSort() { Calendar time1 = Calendar.getInstance(); int sec1 = time1.get(Calendar.MILLISECOND); System.out.println("selectionSort"); for(int i = 0;i<data.length;i++) { int minIdex = i; for( int j = i+1;j<data.length;j++) if (data[j]<data[minIdex]) minIdex = j; if(minIdex != i) swap(data,minIdex,i); } Calendar time2 = Calendar.getInstance(); int sec2 = time2.get(Calendar.MILLISECOND);// System.out.println("selectionSort used the millisec is"+(sec2-sec1)); } public void bubbleSort() { Calendar time5 = Calendar.getInstance(); int sec5 = time5.get(Calendar.MILLISECOND); System.out.println("bubbleSort"); for(int i = 0;i<data.length-1;i++) { boolean swapped = false; for(int j = 0;j<data.length-i-1;j++) if(data[j]>data[j+1]) { swap(data,j,j+1); swapped = true; } if(!swapped) return; } Calendar time6 = Calendar.getInstance(); int sec6 = time6.get(Calendar.MILLISECOND);// System.out.println("bubbleSortused the millisec is"+(sec6-sec5)); } public void insertionSort() { Calendar time3 = Calendar.getInstance(); int sec3 = time3.get(Calendar.MILLISECOND); System.out.println("insertionSort"); for(int i = 1;i<data.length;i++) { int itemToInsert = data[i]; int j = i-1; while(j>=0) { if(itemToInsert<data[j]) { data[j+1] = data[j]; j--; } else break; } data[j+1] = itemToInsert; } Calendar time4 = Calendar.getInstance(); int sec4 = time4.get(Calendar.MILLISECOND);// System.out.println("insertionSort used the millisec is"+(sec4-sec3)); } public void run() { synchronized (this.name) { if(this.name.equals("selectionSort")) selectionSort(); else if(this.name.equalsIgnoreCase("bubleSort")) bubbleSort(); else if(this.name.equals("insertSort")) insertionSort(); } } public static void main(String args[]) { TestSort p1 = new TestSort("selectionSort"); TestSort p2 = new TestSort("bubleSort"); TestSort p3 = new TestSort("insertSort"); p1.setPriority(Thread.MAX_PRIORITY); p1.setPriority(Thread.NORM_PRIORITY); p1.setPriority(Thread.MIN_PRIORITY); p1.start(); p2.start(); p3.start(); }} 结果是随机调度线程???
解决方案
还一个问题,你的程序中 TestSort p1 = new TestSort("selectionSort"); TestSort p2 = new TestSort("bubleSort"); TestSort p3 = new TestSort("insertSort");也不会发生数据同步访问的可能性,你这里synchronized (this.name) { if(this.name.equals("selectionSort")) selectionSort(); else if(this.name.equalsIgnoreCase("bubleSort")) bubbleSort(); else if(this.name.equals("insertSort")) insertionSort(); }同步操作没什么意义。楼主还得多理解下线程使用的场景。
解决方案二:
线程调度器挑选一个新的线程时, 会优先考虑高优先级的线程,但在Java中,线程的实现机制依赖于底层的操作系统,这种设置 p1.setPriority(Thread.MAX_PRIORITY); p1.setPriority(Thread.NORM_PRIORITY); p1.setPriority(Thread.MIN_PRIORITY);可能在有些平台上就失效了,如果优先级相同,就进行随机调度线程了。所以程序的正确性是不应该建立在依赖线程的优先级的。还有这个 p1.setPriority(Thread.MAX_PRIORITY); p1.setPriority(Thread.NORM_PRIORITY); p1.setPriority(Thread.MIN_PRIORITY);三个p1在设置优先级,是啥意思?
解决方案三:
结果会是随机的调度线程,因为线程的优先级是在调用yeild让位方法的时候能体现出来,在调用start方法后线程就处于就绪状态,是可以参与竞争CPu 的,因此他们是随机调度的, 调用yeild方法则本线程会向比它优先级高或者跟他优先级相同的线程让位,但是本线程也是要参与竞争的这是我的个人见解,仅供参考