Created
October 20, 2014 22:39
-
-
Save OlgaKulikova/9ae92fb3a1b9b958a85c to your computer and use it in GitHub Desktop.
Thread-Counter
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; | |
// Модифицировать класс Counter так, чтобы он циклически выводил числа из определенного диапазона. | |
public class Main { | |
public static class Counter extends Thread { | |
int from, to, sleep; | |
public Counter (int from, int to, int sleep) { | |
this.from = from; | |
this.to = to; | |
this.sleep = sleep; | |
} | |
public void run() { | |
try { | |
for (int i = from; i <= to; i++) { | |
System.out.println(i); | |
Thread.sleep(sleep); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
int from = 100, to = 200, sleep = 500; | |
try { | |
Counter c = new Counter(from, to, sleep); | |
c.start(); // запускаем поток | |
c.join(); // ждем завершения потока | |
System.out.println("Thread finished"); | |
} catch (Exception e) { | |
; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment