今天写个DEMO遇到一个关于try的圆括号语法的问题,求教。
下面是两种代码写法,一个使用了try的圆括号语法,另外一个直接让main()方法抛出Exception。其他业务逻辑没有变动,但是第一种使用了try的代码就能够成功往文件写入内容,第二种就只会创建一个空白文件,不会向文件写入内容。特此求教。
第一种,使用了try的代码:
public static void main(String[] args) {
Thread[] threads = new Thread[10];
Thread.State status[] = new Thread.State[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread(new Calculator(i));
if (i % 2 == 0) {
threads[i].setPriority(Thread.MAX_PRIORITY);
} else {
threads[i].setPriority(Thread.MIN_PRIORITY);
}
threads[i].setName("Thread" + i);
}
try (
FileWriter file = new FileWriter("/Users/jasongan/Desktop/log.txt");
PrintWriter pw = new PrintWriter(file);
) {
for (int i = 0; i < 10; i++) {
pw.println("Main : Status of Thread " + i + " : " + threads[i].getState());
status[i] = threads[i].getState();
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
boolean finish = false;
while (!finish) {
for (int i = 0; i < 10; i++) {
if (threads[i].getState() != status[i]) {
writeThreadInfo(pw, threads[i], status[i]);
status[i] = threads[i].getState();
}
}
finish = true;
for (int i = 0; i < 10; i++) {
finish = finish && (threads[i].getState() == State.TERMINATED);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void writeThreadInfo(PrintWriter pw, Thread thread, State state) {
pw.printf("Main : ID %d - %s\n", thread.getId(), thread.getName());
pw.printf("Main : Priority: %d\n", thread.getPriority());
pw.printf("Main : Old State: %s\n", state);
pw.printf("Main : New State: %s\n", thread.getState());
pw.printf("Main : *****************************\n");
}
第二种,直接让main()方法抛出异常:
public static void main(String[] args) throws Exception {
Thread[] threads = new Thread[10];
Thread.State status[] = new Thread.State[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread(new Calculator(i));
if (i % 2 == 0) {
threads[i].setPriority(Thread.MAX_PRIORITY);
} else {
threads[i].setPriority(Thread.MIN_PRIORITY);
}
threads[i].setName("Thread" + i);
}
FileWriter file = new FileWriter("/Users/jasongan/Desktop/log.txt");
PrintWriter pw = new PrintWriter(file);
for (int i = 0; i < 10; i++) {
pw.println("Main : Status of Thread " + i + " : " + threads[i].getState());
status[i] = threads[i].getState();
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
boolean finish = false;
while (!finish) {
for (int i = 0; i < 10; i++) {
if (threads[i].getState() != status[i]) {
writeThreadInfo(pw, threads[i], status[i]);
status[i] = threads[i].getState();
}
}
finish = true;
for (int i = 0; i < 10; i++) {
finish = finish && (threads[i].getState() == State.TERMINATED);
}
}
}
private static void writeThreadInfo(PrintWriter pw, Thread thread, State state) {
pw.printf("Main : ID %d - %s\n", thread.getId(), thread.getName());
pw.printf("Main : Priority: %d\n", thread.getPriority());
pw.printf("Main : Old State: %s\n", state);
pw.printf("Main : New State: %s\n", thread.getState());
pw.printf("Main : *****************************\n");
}
------解决思路----------------------
因为try会为圆括号中的资源自动添加finally块,finally块内写的是close圆括号中的资源。这要求圆括号中的资源必须是auto-closeable的。
第二次没有内容输出到文件的原因是程序抛出了异常,但是流没有来得及被close,程序就结束了。在缓冲区内的内容还没有来得及被写入文件。第一次由于finally块的存在使得流在抛出异常后程序终止前能被关闭。
------解决思路----------------------
try圆括号是jdk1.7的新特性,try语法结束后会自动释放()内的资源,此时也就是流的close操作。第二种没有关流,也没有执行flush操作,数据并没有写入文件中