synchronized int getNextFreePort() {

		while (true) {

			// Acquire a control port
			while (myCurrentControlSocketPort == null) {
				int nextCandidate = (int) (Math.random() * 65000.0);
				nextCandidate = nextCandidate - (nextCandidate % SPACE_SIZE);

				if (nextCandidate < 10000) {
					continue;
				}

				try {
					ServerSocket server = new ServerSocket();
					server.setReuseAddress(true);
					server.bind(new InetSocketAddress("localhost", nextCandidate));
					myControlSockets.add(server);
					ourLog.info("Acquired control socket on port {}", nextCandidate);
					myCurrentControlSocketPort = nextCandidate;
					myCurrentOffset = 0;
				} catch (IOException theE) {
					ourLog.info("Candidate control socket {} is already taken", nextCandidate);
					continue;
				}
			}

			// Find a free port within the allowable range
			while (true) {
				myCurrentOffset++;

				if (myCurrentOffset == SPACE_SIZE) {
					// Current space is exhausted
					myCurrentControlSocketPort = null;
					break;
				}

				int nextCandidatePort = myCurrentControlSocketPort + myCurrentOffset;

				// Try to open a port on this socket and use it
				if (!isAvailable(nextCandidatePort)) {
					continue;
				}

				// Log who asked for the port, just in case that's useful
				StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
				StackTraceElement previousElement = Arrays.stream(stackTraceElements)
					.filter(t -> !t.toString().contains("PortUtil.") && !t.toString().contains("getStackTrace"))
					.findFirst()
					.orElse(stackTraceElements[2]);
				ourLog.info("Returned available port {} for: {}", nextCandidatePort, previousElement.toString());

				try {
					Thread.sleep(ourPortDelay);
				} catch (InterruptedException theE) {
					// ignore
				}

				return nextCandidatePort;

			}


		}
	}
synchronized int getNextFreePort ( ) { while ( true ) { while ( myCurrentControlSocketPort == null ) { int nextCandidate = ( int ) ( Math . random ( ) * 65000.0 ) ; nextCandidate = nextCandidate - ( nextCandidate % SPACE_SIZE ) ; if ( nextCandidate < 10000 ) { continue ; } try { ServerSocket server = new ServerSocket ( ) ; server . setReuseAddress ( true ) ; server . bind ( new InetSocketAddress ( "localhost" , nextCandidate ) ) ; myControlSockets . add ( server ) ; ourLog . info ( "Acquired control socket on port {}" , nextCandidate ) ; myCurrentControlSocketPort = nextCandidate ; myCurrentOffset = 0 ; } catch ( IOException theE ) { ourLog . info ( "Candidate control socket {} is already taken" , nextCandidate ) ; continue ; } } while ( true ) { myCurrentOffset ++ ; if ( myCurrentOffset == SPACE_SIZE ) { myCurrentControlSocketPort = null ; break ; } int nextCandidatePort = myCurrentControlSocketPort + myCurrentOffset ; if ( ! isAvailable ( nextCandidatePort ) ) { continue ; } StackTraceElement [ ] stackTraceElements = Thread . currentThread ( ) . getStackTrace ( ) ; StackTraceElement previousElement = Arrays . stream ( stackTraceElements ) . filter ( t -> ! t . toString ( ) . contains ( "PortUtil." ) && ! t . toString ( ) . contains ( "getStackTrace" ) ) . findFirst ( ) . orElse ( stackTraceElements [ 2 ] ) ; ourLog . info ( "Returned available port {} for: {}" , nextCandidatePort , previousElement . toString ( ) ) ; try { Thread . sleep ( ourPortDelay ) ; } catch ( InterruptedException theE ) { } return nextCandidatePort ; } } }
Clear and release all control sockets