autocloseable

Close multiple resources with AutoCloseable (try-with-resources)

蹲街弑〆低调 提交于 2019-11-27 18:41:10
I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want automatically closed. Example with sockets; try (Socket socket = new Socket()) { input = new DataInputStream(socket.getInputStream()); output = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { } So I know the socket will be closed properly, because it's passed as a parameter in the try, but how should the input and output be closed properly? augray Try with resources can be

What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 01:03:59
问题 Java docs of close() method of java.lang.AutoCloseable says Note that unlike the close() method of Closeable, this close() method is not required to be idempotent . In other words, calling this close method more than once may have some visible side effect, unlike Closeable#close() which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent. What do they mean by idempotent method and what

Close multiple resources with AutoCloseable (try-with-resources)

半腔热情 提交于 2019-11-26 19:32:53
问题 I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want automatically closed. Example with sockets; try (Socket socket = new Socket()) { input = new DataInputStream(socket.getInputStream()); output = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { } So I know the socket will be closed properly, because it's passed as a parameter