Created
June 8, 2018 21:54
-
-
Save paullewallencom/efb07792a5e87643f03944200458a804 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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