睡眠排序法

public static void main(String[] args) {

        int[] arr = {1,4,7,3,8,9,2,6,5};
        //創建指定長度的線程數組
        SThread[] threads = new SThread[arr.length];
        //指定每個線程數組的值
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new SThread(arr[i]);
        }
        //開啓每個線程
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
        }
    }

    static class SThread extends Thread{
        int s = 0;
        public SThread(int s){
            this.s = s;
        }
        public void run(){
            try {
                sleep(s);  //睡眠指定的時間
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //輸出該數
            System.out.println(s);
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章