Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class ConsoleClock implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.printf("%s\n", new Date());
try {
// Sleep during one second
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.printf("The FileClock has been interrupted.\n");
}
}
}
}
// Sleeping and resuming a thread
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
// Creates a FileClock runnable object and a Thread
// to run it
ConsoleClock clock = new ConsoleClock();
Thread thread = new Thread(clock);
// Starts the Thread
thread.start();
try {
// Waits five seconds
TimeUnit.SECONDS.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Interrupts the Thread
thread.interrupt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment