"Java records" a pit of try-with-resources
Today, when dealing with a problem with Asynchronousfilechannel, the code is as follows:
public static void Main (string[] args) throws Exception {String FilePath = "/HOME/XE/GIT/OSC/JAVANOTE/LANG/DATA/TEST.J Ava "; Executorservice Executorservice = Executors.newsinglethreadexecutor (); set<openoption> openoptions = new Hashset<> (arrays.aslist (New standardopenoption[]{ Standardopenoption.read})); Try (Asynchronousfilechannel Asynchronousfilechannel = Asynchronousfilechannel.open (Paths.get (FilePath), Openoptions, Executorservice)) {Bytebuffer buffer = bytebuffer.allocate (1024); Asynchronousfilechannel.read (buffer, 0, buffer, new Completionhandler<integer, bytebuffer> () {@Override public void completed (Integer result, bytebuffer attachment) {System.out.println ("Completed,res Ult = "+ result"; Executorservice.shutdown (); } @Override public void failed (Throwable exc, bytebuffer attachment) {System.out.pri Ntln ("failed");Exc.printstacktrace (); Executorservice.shutdown (); } }); } catch (Exception e) {}}
Always prompt:
java.nio.channels.AsynchronousCloseException
have been unable to find the cause of the problem, because I did not explicitly to close the Asynchronousfilechannel, later found that is the Try-with-resources feature to the closed, sad story.
Therefore, it is better to handle the closed operation of the advertisement in the asynchronous call or in a cautious process.
"Java records" a pit of try-with-resources