Skip to content

Instantly share code, notes, and snippets.

@kirtideshmukh
Forked from utsengar/Sleepsort.java
Created June 23, 2018 06:29
Show Gist options
  • Save kirtideshmukh/5073385788d540150d8f9e384b9d8bad to your computer and use it in GitHub Desktop.
Save kirtideshmukh/5073385788d540150d8f9e384b9d8bad to your computer and use it in GitHub Desktop.
Java version of Sleep sort
/**
* For fun.
* Inspiration: http://dis.4chan.org/read/prog/1295544154
* @author zengr
*
*/
public class Sleepsort {
private static int[] inputArray = { 3, 1, 2, 1, 181, 10};
public static void main(String[] args) throws InterruptedException {
Sleepsort ss = new Sleepsort();
ss.sleepsort(inputArray);
}
public void sleepsort(int[] array) throws InterruptedException {
for (int val : array) {
Thread thread = new Thread(new SleepsortThread(val));
thread.start();
}
}
}
class SleepsortThread implements Runnable {
private int val;
public SleepsortThread(int val) {
this.val = val;
}
@Override
public void run() {
try {
Thread.sleep(val);
System.out.println(val);
} catch (InterruptedException e) {
// Oops...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment