Last active
August 29, 2015 14:07
-
-
Save OlgaKulikova/052d6cc7960a3e54b15f to your computer and use it in GitHub Desktop.
Thread4
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.Task4; | |
// Создать поток, который создаст 50 потоков, | |
// каждый их которых выведет на экран свой номер и дождется, пока его прервут. | |
// Прерывание дочерних потоков должно выполнятся из потока их порождающего. | |
public class Main { | |
public static void main(String[] args) { | |
MyThread mt = new MyThread(); | |
mt.start(); | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
mt.interrupt(); | |
System.out.println("MainTread finished!"); | |
} | |
} |
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.Task4; | |
import java.util.ArrayList; | |
public class MyThread extends Thread { | |
@Override | |
public void run() { | |
ArrayList<NewThreads> threads = new ArrayList<>(); | |
for (int i = 0; i < 50; i++) { | |
NewThreads mt2 = new NewThreads(); | |
mt2.start(); | |
threads.add(mt2); | |
} | |
while (! isInterrupted()) { | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
break; | |
} | |
} | |
for (NewThreads list : threads) { | |
list.interrupt(); | |
} | |
System.out.println("MyThread finished!"); | |
} | |
} |
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.Task4; | |
public class NewThreads extends Thread { | |
@Override | |
public void run() { | |
System.out.println(getId() + " started"); | |
while (! isInterrupted()) { | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
break; | |
} | |
} | |
System.out.println(getId() + " interrupted"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment