Created
October 20, 2014 23:43
-
-
Save OlgaKulikova/e4d1b19d4b42f33af09e to your computer and use it in GitHub Desktop.
Thread3
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.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(); | |
} | |
} | |
} |
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.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