f*******y 发帖数: 988 | |
w***g 发帖数: 5958 | 2 java里面要explicitly close。C++里那讨RAII貌似在java里不行(不确信,希望java
高手指正).
python不知道。
【在 f*******y 的大作中提到】 : 比如文件和socket?
|
g*****g 发帖数: 34805 | 3 In java, you are still recommended to manually close an IOStream.
In certain case, you want to do it in a finally block,
that prevents some problem you may see in C++ coz the block is
guarranteed to be executed. It's also notable that since GC will reclaim an
non-referenced instance automatically, this is often not an issue even if
you didn't do that.
【在 f*******y 的大作中提到】 : 比如文件和socket?
|
f*******y 发帖数: 988 | 4 用finally是一个好的做法呢?
貌似google一下有人不太建议
an
【在 g*****g 的大作中提到】 : In java, you are still recommended to manually close an IOStream. : In certain case, you want to do it in a finally block, : that prevents some problem you may see in C++ coz the block is : guarranteed to be executed. It's also notable that since GC will reclaim an : non-referenced instance automatically, this is often not an issue even if : you didn't do that.
|
g*****g 发帖数: 34805 | 5 sure finally is useful, check
http://www.ibm.com/developerworks/java/library/j-praxis/pr21.html
【在 f*******y 的大作中提到】 : 用finally是一个好的做法呢? : 貌似google一下有人不太建议 : : an
|
r****t 发帖数: 10904 | 6 In Python, everything (file descriptor, sockets, etc etc...) is automatically closed/GCed when the last name referring to it goes out of its scope.
但是要记得 shutdown socket。
socket is a different story. Supposedly you should shutdown() and then close() your connection. So, imagine you forgot about one socket you created, when it is GCed, socket.close() is automatically called. That'll close your side of the connection (you are fine) but leave the other side of connection wait forever: because he/s
【在 f*******y 的大作中提到】 : 比如文件和socket?
|
d***q 发帖数: 1119 | 7 python adopt handle counting technique for resource management.
when the count of object becomes zero, it will be released automatically.
so even you don't close a opened file object manually, it will be close when
reaching the end of block
for Jython it is another story. |
h*********o 发帖数: 62 | 8 in java, you should always release scarce non-memory resources in your code
explicitly without relying on GC. |
a**a 发帖数: 416 | 9 In python 2.5,
from __future__ import with_statement
You can write a special objective working with the `with' statement, such as
with SomeClass() as obj:
## do you things here
## when leaving the current context normally or by exception,
## the obj's speciall method will be invoked and do the house
## cleaning job. |
a**a 发帖数: 416 | 10 For example,
with file('foo.bar') as foobar:
for line in foorbar:
print line
as
【在 a**a 的大作中提到】 : In python 2.5, : from __future__ import with_statement : You can write a special objective working with the `with' statement, such as : with SomeClass() as obj: : ## do you things here : ## when leaving the current context normally or by exception, : ## the obj's speciall method will be invoked and do the house : ## cleaning job.
|
r****t 发帖数: 10904 | 11 这个 with statement 今天俺也贴了一个
【在 a**a 的大作中提到】 : For example, : with file('foo.bar') as foobar: : for line in foorbar: : print line : : as
|
h***i 发帖数: 1970 | 12 对,其实很像c++的shared_ptr
when
【在 d***q 的大作中提到】 : python adopt handle counting technique for resource management. : when the count of object becomes zero, it will be released automatically. : so even you don't close a opened file object manually, it will be close when : reaching the end of block : for Jython it is another story.
|