> Would this be a good candidate to use Automatic Resource Management ?

Correct. I'll remember to use it the next time.

Thanks
Max

On 06/13/2012 08:23 PM, Neil Richards wrote:
On Wed, 2012-06-13 at 17:23 +0800, Weijun Wang wrote:
Please anyone take a review:

     http://cr.openjdk.java.net/~weijun/7176574/webrev.00/

By assigning to a local variable hopefully it stays alive on the stack
during the whole method.

Noreg-self.

*Chris*: I didn't indented the whole test by wrapping them into a
try-finally (or try-with-resources) block. The test runs in othervm and
I guess the sockets will be closed anyway.

Thanks
Max

On 06/13/2012 05:08 PM, Chris Hegarty wrote:


On 13/06/2012 09:51, Alan Bateman wrote:
On 13/06/2012 09:38, Weijun Wang wrote:
Hi All

I have a test that basically looks like:

int p = new ServerSocket(0).getLocalPort();
//....
new Socket("localhost", p);

Recently it's failing on solaris-i586, and after some investigation, I
realize that the ServerSocket object is GC'ed and auto-closed.

(But why only recently?)

So I change the first line to

ServerSocket ss = new ServerSocket(0);
int p = ss.getLocalPort();

and it's running fine.

I want to know if the ServerSocket object still has a chance to be
closed. If yes, I'll add a

ss.close();

at the end to be safer.

Thanks
Max
HotSpot changes I assume, perhaps changes to the reference processing or
default heap settings.

Right, I assume there was some VM change that started this test to fail
recently, but clearly this is a test issue. It was just passing all this
time by accident, and there is an inherent race between the test and the
GC/finalizer thread.

You should fix the test as you suggested. Also close the serversocket in
a finally block ( or equivalent ). You should not rely on the finalizer
to close it out.

-Chris.


-Alan

Would this be a good candidate to use Automatic Resource Management ?

i.e. instead of doing this:

         ServerSocket ss1 = null;
         ServerSocket ss2 = null;
         try {
                 ss1 = new ServerSocket(0);
                 ss2 = new ServerSocket(0);
                 int p1 = ss1.getLocalPort();
                 int p2 = ss1.getLocalPort();
                 //...
         } finally {
                 if (ss1 != null) ss1.close();
                 if (ss2 != null) ss2.close();
         }

one would do this:

         try (ServerSocket ss1 = new ServerSocket(0);
                         ServerSocket ss2 = new ServerSocket(0)) {
                 int p1 = ss1.getLocalPort();
                 int p2 = ss1.getLocalPort();
                 //...
         }

Regards,
Neil

Reply via email to