题解 | #多线程#
多线程
http://www.nowcoder.com/questionTerminal/cd99fbc6154d4074b4da0e74224a1582
使用join函数
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main implements Runnable {
public static String s = new String();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
String[] s2 = {"A", "B", "C", "D"};
Runnable runnable = new Main();
for (int i = 0; i < n; i++) {
for (int j = 0; j < s2.length; j++) {
Thread thread = new Thread(runnable);
s = s2[j];
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println();
}
}
@Override
public void run() {
System.out.print(s);
}
}