boxingcai 2011-04-17 19:57
浏览 197
已采纳

线程优先级问题


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();
    }
}

 

 

 

结果是随机调度线程???

  • 写回答

3条回答 默认 最新

  • lxbccsu 2011-04-18 12:52
    关注

    还一个问题,你的程序中[code="java"] TestSort p1 = new TestSort("selectionSort");
    TestSort p2 = new TestSort("bubleSort");
    TestSort p3 = new TestSort("insertSort");

    [/code]也不会发生数据同步访问的可能性,你这里[code="java"]synchronized (this.name) {
    if(this.name.equals("selectionSort"))
    selectionSort();
    else if(this.name.equalsIgnoreCase("bubleSort"))
    bubbleSort();
    else if(this.name.equals("insertSort"))
    insertionSort();
    }
    [/code]同步操作没什么意义。
    楼主还得多理解下线程使用的场景。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计