日期:2014-05-20 浏览次数:21038 次
public void dosomething(){
for(i=0;i<5;i++){
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
Socket socket = new Socket("127.0.0.1",portarr[i]);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
).start();
}
}
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyThread extends Thread
{
private int port;
public MyThread(int port)
{
this.port = port;
}
@Override
public void run()
{
try
{
Socket socket = new Socket("127.0.0.1", port);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class ThreadPool
{
public void init()
{
int[] portarr = {10000, 10001, 10002, 10003, 10004};
for (int i = 0; i < 5; i++)
{
new MyThread(portarr[i]).start();
}
}
}