Created
February 1, 2016 12:27
-
-
Save Viacheslav77/26ed5762a8737eb00dc9 to your computer and use it in GitHub Desktop.
1.Модифицировать класс Counter так, чтобы он циклически выводил числа из определенного диапазона. 2. Создать поток, который будет каждые 10 секунд выводить текущее время на экран. Сделать возможность прерывания потока с помощью команды с консоли.
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 Counter; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import java.util.Scanner; | |
| public class CounterTime extends Thread { | |
| public void run() { | |
| int x = 1; | |
| while (!isInterrupted()) { // условие завершения потока №1 | |
| long t=System.currentTimeMillis(); | |
| Date d = new Date (t); | |
| SimpleDateFormat sdf = new SimpleDateFormat (" HH:mm:ss"); | |
| System.out.println(getId() + ": " + sdf.format(d) + " Если хотите прервать поток введите 'n' "); | |
| try { | |
| Thread.sleep(1000); | |
| } catch (InterruptedException e) { | |
| //System.out.println(); | |
| return ; // условие завершения потока №2 | |
| } | |
| } | |
| } | |
| } | |
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 Counter; | |
| import java.util.Date; | |
| import java.util.Scanner; | |
| /* | |
| 1.Модифицировать класс Counter так, чтобы он циклически выводил | |
| числа из определенного диапазона. | |
| 2. Создать поток, который будет каждые 10 секунд выводить текущее | |
| время на экран. Сделать возможность прерывания потока с помощью | |
| команды с консоли. | |
| */ | |
| public class Main { | |
| public static class Counter extends Thread { | |
| public void run() { | |
| int x = 1; | |
| while (!isInterrupted()) { // условие завершения потока №1 | |
| System.out.print(getId() + ": " + x++ + " "); | |
| for(int i=1 ; i<10; i++) | |
| System.out.print(i); | |
| System.out.println(); | |
| try { | |
| Thread.sleep(1000); // закомментировать | |
| // Thread.yield(); // или так | |
| } catch (InterruptedException e) { | |
| //System.out.println(); | |
| return ; // условие завершения потока №2 | |
| } | |
| } | |
| } | |
| } | |
| public static void main(String[] args) { | |
| System.out.println("\n2. Создать поток, который будет каждые 10 секунд выводить текущее время на экран. Сделать возможность прерывания потока с помощью команды с консоли.\n"); | |
| Scanner sc = new Scanner(System.in); | |
| try { | |
| CounterTime c1 = new CounterTime(); | |
| c1.start(); // запускаем поток | |
| if( sc.nextLine().equals("n")) | |
| c1.interrupt(); // прерываем поток | |
| } catch (Exception e) { | |
| ; | |
| } | |
| System.out.println("\n1.Модифицировать класс Counter так, чтобы он циклически выводил числа из определенного диапазона.\n"); | |
| try { | |
| Counter c = new Counter(); | |
| Date d = new Date(); | |
| c.start(); // запускаем поток | |
| Thread.sleep(5000); // ждем 5 сек | |
| c.interrupt(); // прерываем поток | |
| } catch (Exception e) { | |
| ; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment