Java 线程的基本信息

《java 7 concurrency cookbook》这本书看了第一节就被书中的系列错误和忽略点给吓哭了。
首先写文件
PrintWrite pw =  new PrintWriter(new FileWriter("D:\\log.txt"));
这个没问题,但是没有在加上pw.close()
flush什么的就不说了,不过printwriter是可以设置成自动刷新的,为什么要flush还是引用下stackoverflow上的一个问题吧
   
   
  1. PrintWriter(OutputStream out, boolean autoFlush)

flush() is probably not required in your example.

What it does is ensure that anything written to the writer prior to the call to flush() is written to the underlying stream, rather than sit in some internal buffer.

The method comes in handy in several types of circumstances:

  1. If another process (or thread) needs to examine the file while it's being written to, and it's important that the other process sees all the recent writes.

  2. If the writing process might crash, and it's important that no writes to the file get lost.

  3. If you're writing to the console, and need to make sure that every message is shown as soon as it's written.


     

JavaDoc:
Prints formatted representations of objects to a text-output stream. This class implements all of the  print  methods found in  PrintStream . It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the printlnprintf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().




正好在stackoverflow上也有人提问过这个问题:

PrintWriter swallows exceptions. See the Javadoc. You need to call checkError(). Better still, use BufferedWriter, which doesn't swallow them. You're probably running up against an apple sandbox permission problem unless your applet is signed.

 
所以结果是不要用printwriter,一本讲java7的内容的居然还在用printwriter


第一节 充其量也只是讲了怎么设置线程name,怎么获取线程status以及设置线程优先级。书中也并没有讲到高优先级的线程不一定比低优先级线程 优先执行,只是优先级越高,执行几率就越大
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值