所以我正在修改一些代码。无需详细了解其实际用途,只需创建一个文件然后将其发送到打印机即可对其进行总结。基本上如下:
File file = new File("/tmp/12345.pdf");
//Lots of magical code that creates/writes to said file
...
...
...
//sendToPrinter essentially builds up a print command to send to /usr/bin/lp and then executes it.
sendToPrinter(printer, details, file);
file.delete();// This is the line I'm curious about.
我的问题是对 lp 的调用是在我们到达 file.delete() 之前进行的。但是,这是否存在创建竞争条件的风险,即我实际上最终在打印机准备就绪之前删除了文件?
从初步测试来看,我还没有发现问题,但这件事让我感到困扰。我这样做是否造成了竞争条件的风险?
编辑:根据评论进行澄清。是的,我正在使用 lp 并且 sendToPrinter 似乎正在等待返回代码。该代码不是异步的。
最佳答案
来自文档
The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,
t.join(); causes the current thread to pause execution until t's thread terminates.
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
关于java - 打印机竞争状况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829779/