Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Created October 20, 2014 22:39
Show Gist options
  • Save OlgaKulikova/9ae92fb3a1b9b958a85c to your computer and use it in GitHub Desktop.
Save OlgaKulikova/9ae92fb3a1b9b958a85c to your computer and use it in GitHub Desktop.
Thread-Counter
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