Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Created October 20, 2014 23:43
Show Gist options
  • Save OlgaKulikova/e4d1b19d4b42f33af09e to your computer and use it in GitHub Desktop.
Save OlgaKulikova/e4d1b19d4b42f33af09e to your computer and use it in GitHub Desktop.
Thread3
package Threads.Task2.Task3;
// Создать 100 потоков, каждый их которых выведет на экран свой номер и дождется, пока его прервут.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<MyThread> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
MyThread thr = new MyThread();
thr.start();
list.add(thr);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (MyThread myThread : list) {
myThread.interrupt();
}
}
}
package Threads.Task2.Task3;
public class MyThread extends Thread{
@Override
public void run() {
System.out.println(getId() + "started");
while (! isInterrupted()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(getId() + "interrupted");
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment