提示”threads 列表中的线程 3 应每半秒显示一次“加油”。“, 为什么?
package zh.codegym.task.task16.task1632;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static List<Thread> threads = new ArrayList<>(5);
static{
threads.add(new testThread_1());
threads.add(new testThread_2());
threads.add(new testThread_3());
threads.add(new testThread_4());
threads.add(new testThread_5());
}
public static void main(String[] args) {
}
public static class testThread_1 extends Thread{
public void run(){
try {
while (true){
Thread.sleep(100);
}
}
catch (InterruptedException e){
}
}
}
public static class testThread_2 extends Thread{
public void run(){
try {
while (true){
Thread.sleep(100);
}
}
catch (InterruptedException e){
System.out.println("InterruptedException");
}
}
}
public static class testThread_3 extends Thread{
public void run(){
try {
while (true){
Thread.sleep(500);
System.out.println("加油");
}
}catch (InterruptedException e){
}
}
}
public static class testThread_4 extends Thread implements Message {
public void run(){
try {
while (isAlive()){
Thread.sleep(500);
}
}
catch (InterruptedException e){
return;
}
}
public void showWarning(){
interrupt();
}
}
public static class testThread_5 extends Thread{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static int sum = 0;
public void run(){
try {
String s = null;
while (true){
s = reader.readLine();
if (s.equals("N"))
{
System.out.println(sum);
return;
}
sum += Integer.parseInt(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}