From 35582ded0e6619fa0d86fe1eb0182274456767b7 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 3 Jul 2020 12:04:02 +0200 Subject: [PATCH] Use System.nanoTime() for Random seed in SocketUtils Prior to this commit, SocketUtils used System.currentTimeMillis() for the seed for the java.util.Random instance used internally. The use of the milliseconds value returned by currentTimeMillis() can lead to collisions for randomly selected free ports for tests executing in parallel on the same computer. This commit therefore switches to System.nanoTime() for the Random seed used in SocketUtils in an attempt to avoid such collisions for tests executing in parallel in different JVMs on the same computer. Closes gh-25321 --- .../src/main/java/org/springframework/util/SocketUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/SocketUtils.java b/spring-core/src/main/java/org/springframework/util/SocketUtils.java index 4362e94e199..557173e7e59 100644 --- a/spring-core/src/main/java/org/springframework/util/SocketUtils.java +++ b/spring-core/src/main/java/org/springframework/util/SocketUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ public class SocketUtils { public static final int PORT_RANGE_MAX = 65535; - private static final Random random = new Random(System.currentTimeMillis()); + private static final Random random = new Random(System.nanoTime()); /**