• 算法-睡眠算法


    第一种

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

      

  • 相关阅读:
    NSString 处理
    我的第一个IOSDemo
    NSArray创建和使用
    NSDate
    NSDictionary
    flash全屏代码
    getBounds
    运用递归随机出与上一个数不重复的数
    标签跟随鼠标移动
    保存数据到本地
  • 原文地址:https://www.cnblogs.com/zhang20190701/p/13436085.html
Copyright © 2020-2023  润新知