控制线程的停止,不推荐使用stop(),因为这个方法会引起严重的系统故障,类似的还有suspend(), 和 resume() 挂起方法 。
推荐做法是
private static String command = "";
while (!command.equalsIgnoreCase("exit") {
//TODO
System.out.println("Downloading...");
try {
//Thread 休眠
Thread.sleep(1000);
}
catch (InterruptedExpection e) {
}
...
=======
下面try的()中的代码一般放的是对资源的申请,如果{}中的代码出项了异常,()中的资源就会被关闭,这在inputstream和outputstream的使用中会很方便例如
try(
Connection conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement()
) {
boolean hasResultSet=stmt.execute(sql);
}
Reference:
Java中try()catch{}的使用方法 :https://blog.youkuaiyun.com/qq_33543634/article/details/80725899