Skip to content

Instantly share code, notes, and snippets.

@paullewallencom
Created June 8, 2018 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paullewallencom/efb07792a5e87643f03944200458a804 to your computer and use it in GitHub Desktop.
Save paullewallencom/efb07792a5e87643f03944200458a804 to your computer and use it in GitHub Desktop.
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