我需要在Java中构建一个工作池,每个工作者都有自己的连接套接字;当工作线程运行时,它使用套接字但保持打开以便以后重用。我们决定使用这种方法,因为与临时创建,连接和销毁套接字相关的开销需要太多的开销,所以我们需要一种方法,通过这种方法,工作池预先初始化了它们的套接字连接,准备好承担工作,同时保持套接字资源免受其他线程的影响(套接字不是线程安全的),所以我们需要沿着这些线路......
public class SocketTask implements Runnable {
Socket socket;
public SocketTask(){
//create + connect socket here
}
public void run(){
//use socket here
}
}
在应用程序启动时,我们想要初始化工作者,并希望以某种方式初始化套接字连接......
MyWorkerPool pool = new MyWorkerPool();
for( int i = 0; i < 100; i++)
pool.addWorker( new WorkerThread());
当应用程序请求工作时,我们将任务发送到工作池以立即执行...
pool.queueWork( new SocketTask(..));
更新了工作代码
根据Gray和jontejj的有用评论,我有以下代码工作......
SocketTask
public class SocketTask implements Runnable {
private String workDetails;
private static final ThreadLocal<Socket> threadLocal =
new ThreadLocal<Socket>(){
@Override
protected Socket initialValue(){
return new Socket();
}
};
public SocketTask(String details){
this.workDetails = details;
}
public void run(){
Socket s = getSocket(); //gets from threadlocal
//send data on socket based on workDetails, etc.
}
public static Socket getSocket(){
return threadLocal.get();
}
}
的ExecutorService
ExecutorService threadPool =
Executors.newFixedThreadPool(5, Executors.defaultThreadFactory());
int tasks = 15;
for( int i = 1; i <= tasks; i++){
threadPool.execute(new SocketTask("foobar-" + i));
}
我喜欢这种方法有几个原因......
答案 0 :(得分:10)
一个想法是将Socket
放入BlockingQueue
。然后,当您需要Socket
时,您的线程可以take()
从队列中完成,当他们完成Socket
时,他们put()
就会回到队列中。
public void run() {
Socket socket = socketQueue.take();
try {
// use the socket ...
} finally {
socketQueue.put(socket);
}
}
这有额外的好处:
ExecutorService
代码。ExecutorService
完成后,您可以通过将它们出列并关闭它们来关闭套接字。这会增加另一个BlockingQueue
的额外开销,但如果您正在进行Socket
次通信,则您不会注意到它。
我们不相信ThreadFactory能满足我们的需求......
如果您使用线程本地,我认为您可以完成这项工作。你的线程工厂会创建一个线程,首先打开套接字,将它存储在本地线程中,然后调用Runnable
arg,它使用套接字完成所有工作,从ExecutorService
内部取出作业队列。完成后,arg.run()
方法将完成,您可以从线程本地获取套接字并关闭它。
如下所示。它有点乱,但你应该明白这一点。
ExecutorService threadPool =
Executors.newFixedThreadPool(10,
new ThreadFactory() {
public Thread newThread(final Runnable r) {
Thread thread = new Thread(new Runnable() {
public void run() {
openSocketAndStoreInThreadLocal();
// our tasks would then get the socket from the thread-local
r.run();
getSocketFromThreadLocalAndCloseIt();
}
});
return thread;
}
}));
因此,您的任务将实现Runnable
,如下所示:
public SocketWorker implements Runnable {
private final ThreadLocal<Socket> threadLocal;
public SocketWorker(ThreadLocal<Socket> threadLocal) {
this.threadLocal = threadLocal;
}
public void run() {
Socket socket = threadLocal.get();
// use the socket ...
}
}
答案 1 :(得分:5)
我认为你应该使用ThreadLocal
package com.stackoverflow.q16680096;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main
{
public static void main(String[] args)
{
ExecutorService pool = Executors.newCachedThreadPool();
int nrOfConcurrentUsers = 100;
for(int i = 0; i < nrOfConcurrentUsers; i++)
{
pool.submit(new InitSocketTask());
}
// do stuff...
pool.submit(new Task());
}
}
package com.stackoverflow.q16680096;
import java.net.Socket;
public class InitSocketTask implements Runnable
{
public void run()
{
Socket socket = SocketPool.get();
// Do initial setup here
}
}
package com.stackoverflow.q16680096;
import java.net.Socket;
public final class SocketPool
{
private static final ThreadLocal<Socket> SOCKETS = new ThreadLocal<Socket>(){
@Override
protected Socket initialValue()
{
return new Socket(); // Pass in suitable arguments here...
}
};
public static Socket get()
{
return SOCKETS.get();
}
}
package com.stackoverflow.q16680096;
import java.net.Socket;
public class Task implements Runnable
{
public void run()
{
Socket socket = SocketPool.get();
// Do stuff with socket...
}
}
每个线程都有自己的套接字。