Created
October 20, 2014 22:32
-
-
Save OlgaKulikova/d9f7b9b78197ee58b52c to your computer and use it in GitHub Desktop.
Thread
This file contains hidden or 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
package Threads.Task2; | |
// Создать поток, который будет каждые 10 секунд выводить текущее время на экран. | |
// Сделать возможность прерывания потока с помощью команды с консоли. | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println("Для завершения программы введите слово \"stop\""); | |
Thread thr = new MyThread(); | |
thr.start(); | |
Scanner scanner = new Scanner(System.in); | |
try { | |
while (true) { | |
String s = scanner.nextLine(); | |
if (s.equals("stop")) { | |
thr.interrupt(); | |
break; | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or 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
package Threads.Task2; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class MyThread extends Thread { | |
@Override | |
public void run() { | |
while (! isInterrupted()) { | |
Date curTime = new Date(); | |
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); | |
try { | |
System.out.println(sdf.format(curTime.getTime())); | |
Thread.sleep(10000); | |
} catch (Exception e) { | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment