首页 > 编程语言 > 详细

算法-睡眠算法

时间:2020-08-05 00:15:15      阅读:119      评论:0      收藏:0      [点我收藏+]

第一种

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

  

算法-睡眠算法

原文:https://www.cnblogs.com/zhang20190701/p/13436085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!