import java.util.concurrent.CountDownLatch;
import com.performance.script.script1;
import com.performance.script.script2;
import com.performance.script.script3;
import com.support.*;
public class ThreadsRunnable{
static GetSessionID gsi = new GetSessionID();
static int threadUsers=30;
static Thread thr2;
public void testmain() throws InterruptedException{
// final CountDownLatch latch = new CountDownLatch(1);
for (int i=1; i<=threadUsers; i++) {
Runnable runner = new Runnable() {
public void run() {
try {
Thread.sleep(1000);
// latch.await();
script1 s1 = new script1();
s1.setUp();
s1.testscript1();
cnq.tearDown();
} catch (InterruptedException ie) { } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
new Thread(runner, "TestThread"+i).start();
Thread.sleep(5000);
}
// all threads are waiting on the latch.
// latch.countDown();// release the latch
// all threads are now running concurrently. } public static void main(String arg[]) throws InterruptedException{ ThreadsRunnable tr = new ThreadsRunnable(); tr.testmain(); } }
Below is the code which I run to find if the port 7054 is free and it always display as free on console. However, when i pass this port to run firefox it gives error, port binding can anyone help me on this. Thanks
package GroboUtils;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
/**
* * Finds an available port on localhost.
*/
public class PortFinder {
// the ports below 1024 are system ports private final int
MIN_PORT_NUMBER = 7054;
// the ports above 49151 are dynamic and/or private private final
int MAX_PORT_NUMBER = 49151;
/**
* * Finds a free port between * {@link #MIN_PORT_NUMBER} and
* {@link #MAX_PORT_NUMBER}. * * @return a free port * @throw
* RuntimeException if a port could not be found
*/
public int
findFreePort() {
for (int i = MIN_PORT_NUMBER; i
<= MAX_PORT_NUMBER; i++) {
if (available(i)) {
// System.out.println("Free Port: "+i);
return i;
}
}
throw new RuntimeException("Could not find an available port between"
+ MIN_PORT_NUMBER + " and " + MAX_PORT_NUMBER);
}
/**
* * Returns true if the specified port is available on this host. * *
* @param port the port to check * @return true if the port is available,
* false otherwise
*/
private boolean
available(final int port) {
ServerSocket serverSocket = null;
DatagramSocket dataSocket = null;
try {
serverSocket = new ServerSocket(port);
serverSocket.setReuseAddress(true);
dataSocket = new DatagramSocket(port);
dataSocket.setReuseAddress(true);
return true;
} catch (final IOException e) {
return false;
} finally {
if (dataSocket != null) {
dataSocket.close();
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (final IOException e) {
// can never happen
}
}
}
}
public void portReuse() {
try {
ServerSocket socket1 = new ServerSocket();
ServerSocket socket2 = new ServerSocket();
ServerSocket socket3 = new ServerSocket();
int port = 7054;
socket2.setReuseAddress(true);
socket2.bind(new InetSocketAddress("127.0.0.1", port));
Thread.sleep(Long.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String agr[]) {
PortFinder pf = new PortFinder();
pf.findFreePort();
}
}