//If I had not come and spoken to them, they would not have had sin; but now they have no excuse for their sin. (John 15:22)
package com.javarush.task.task16.task1606;
import java.util.ArrayList;
import java.util.List;
/*
join: в нÑжное вÑÐµÐ¼Ñ Ð² нÑжном меÑÑе
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
PrintListThread firstThread = new PrintListThread("firstThread");
PrintListThread secondThread = new PrintListThread("secondThread");
firstThread.start();
firstThread.join();
secondThread.start();
}
public static void printList(List list, String threadName) {
for (String item : list) {
System.out.println(String.format("%s : %s", threadName, item));
}
}
public static List getList(int n) {
List result = new ArrayList();
if (n < 1) return result;
for (int i = 0; i < n; i++) {
result.add(String.format("String %d", (i + 1)));
}
return result;
}
public static class PrintListThread extends Thread {
public PrintListThread(String name) {
super(name);
}
public void run() {
printList(getList(20), getName());
}
}
}
/*
join: в нÑжное вÑÐµÐ¼Ñ Ð² нÑжном меÑÑе
ÐодÑмай, в каком меÑÑе и Ð´Ð»Ñ ÐºÐ°ÐºÐ¾Ð³Ð¾ обÑекÑа нÑжно вÑзваÑÑ Ð¼ÐµÑод join, ÑÑÐ¾Ð±Ñ ÑезÑлÑÑÐ°Ñ Ð²ÑводилÑÑ Ð¿Ð¾-поÑÑдкÑ
ÑнаÑала Ð´Ð»Ñ firstThread, а поÑом Ð´Ð»Ñ secondThread.
ÐÑзови меÑод join в нÑжном меÑÑе.
ÐÑÐ¸Ð¼ÐµÑ Ð²Ñвода:
firstThread : String 1
firstThread : String 2
...
firstThread : String 19
firstThread : String 20
secondThread : String 1
...
secondThread : String 20
ТÑебованиÑ:
1.âÐеÑод main должен вÑзÑваÑÑ Ð¼ÐµÑод join Ð´Ð»Ñ Ð¾Ð±ÑекÑа firstThread.
2.âÐеÑод main не должен вÑзÑваÑÑ Ð¼ÐµÑод join Ð´Ð»Ñ Ð¾Ð±ÑекÑа secondThread.
3.âÐеÑод main не должен вÑзÑваÑÑ System.out.println() или System.out.print().
4.âÐÑвод пÑогÑÐ°Ð¼Ð¼Ñ Ð´Ð¾Ð»Ð¶ÐµÐ½ ÑооÑвеÑÑÑвоваÑÑ Ð¿ÑимеÑÑ Ð¸Ð· заданиÑ.
package com.javarush.task.task16.task1606;
import java.util.ArrayList;
import java.util.List;
/*
join: в нÑжное вÑÐµÐ¼Ñ Ð² нÑжном меÑÑе
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
PrintListThread firstThread = new PrintListThread("firstThread");
PrintListThread secondThread = new PrintListThread("secondThread");
firstThread.start();
//firstThread.join();
secondThread.start();
}
public static void printList(List list, String threadName) {
for (String item : list) {
System.out.println(String.format("%s : %s", threadName, item));
}
}
public static List getList(int n) {
List result = new ArrayList();
if (n < 1) return result;
for (int i = 0; i < n; i++) {
result.add(String.format("String %d", (i + 1)));
}
return result;
}
public static class PrintListThread extends Thread {
public PrintListThread(String name) {
super(name);
}
public void run() {
printList(getList(20), getName());
}
}
}
*/