• 算法-睡眠算法


    第一种

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

      

  • 相关阅读:
    linux离线安装mysql
    SpringBoot配置SSL证书
    java在liunx下备份mysql数据,恢复数据
    Redis的缓存穿透,缓存击穿,缓存雪崩
    Spring缓存注解
    Advanced Algorithm Scripting
    Arguments Optional
    Everything Be True
    Binary Agents
    Steamroller
  • 原文地址:https://www.cnblogs.com/zhang20190701/p/13436085.html
Copyright © 2020-2023  润新知