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
//所有线程现在同时运行。 } public static void main(String arg [])throws InterruptedException { ThreadsRunnable tr = new ThreadsRunnable(); tr.testmain(); } }
下面是我运行以查找端口7054是否空闲的代码 它总是在控制台上显示为免费。但是,当我通过这个端口 运行firefox它给出错误,端口绑定任何人都可以帮助我 这个。感谢
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();
}
}
答案 0 :(得分:0)
我认为您必须将以下主机条目添加到您的主机文件中。
1270.0.0.1 localhost
它对我有用