问题描述
我在Threads上摔碎了冰块,但因为卡在
按下RETURN键线程结束
 
     但是在typing some string线程时didn't stop 。 
     为什么会这样,因为nexLine()方法采用任何字符串值,为什么在按下RETURN键时线程会停止? 
class MyThread extends Thread {
    private boolean running = true;
    public void run() {
        while (running) {
            System.out.println("hello");
        }
    }
    public void shutdown() {
        running = false;
    }
}
public class Main {
    public static void main(String[] args) {
        MyThread obj = new MyThread();
        obj.start();
        Scanner input = new Scanner(System.in);
        input.nextLine();    
        obj.shutdown();
    }
}
1楼
一旦到达行尾,它就会停止。 输入“ enter”与单击“ \\ n”相同,因此停止。 阅读有关方法和Scanner类的文档可能有助于进一步了解它。
2楼
 
     因为线程将在run方法返回时死亡,而这将在nextLine返回时发生,这将在发生换行符时发生。 
 
     来自Scanner.nextLine() Docs 。 
使该扫描仪前进到当前行之外,并返回被跳过的输入。