raf*_*ian 14 java concurrency thread-local threadpool threadpoolexecutor
我需要在Java中构建一个工作池,每个工作者都有自己的连接套接字; 当工作线程运行时,它使用套接字但保持打开以便以后重用.我们决定使用这种方法,因为与临时创建,连接和销毁套接字相关的开销需要太多的开销,所以我们需要一种方法,通过这种方法,工作池预先初始化了它们的套接字连接,准备好在保持套接字资源不受其他线程影响的同时承担工作(套接字不是线程安全的),所以我们需要这些内容......
public class SocketTask implements Runnable {
Socket socket;
public SocketTask(){
//create + connect socket here
}
public void run(){
//use socket here
}
Run Code Online (Sandbox Code Playgroud)
}
在应用程序启动时,我们想要初始化工作程序,并希望套接字连接在某种程度上......
MyWorkerPool pool = new MyWorkerPool();
for( int i = 0; i < 100; i++)
pool.addWorker( new WorkerThread());
Run Code Online (Sandbox Code Playgroud)
当应用程序请求工作时,我们将任务发送到工作池以立即执行...
pool.queueWork( new SocketTask(..));
Run Code Online (Sandbox Code Playgroud)
更新了工作代码
根据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();
}
}
Run Code Online (Sandbox Code Playgroud)
ExecutorService的
ExecutorService threadPool =
Executors.newFixedThreadPool(5, Executors.defaultThreadFactory());
int tasks = 15;
for( int i = 1; i <= tasks; i++){
threadPool.execute(new SocketTask("foobar-" + i));
}
Run Code Online (Sandbox Code Playgroud)
我喜欢这种方法有几个原因......
Gra*_*ray 10
一个想法是将Sockets放入BlockingQueue.然后,只要你需要一个Socket你的线程就可以take()从队列中完成,当它们完成后,Socket它们put()就会回到队列中.
public void run() {
Socket socket = socketQueue.take();
try {
// use the socket ...
} finally {
socketQueue.put(socket);
}
}
Run Code Online (Sandbox Code Playgroud)
这有额外的好处:
ExecutorService代码.ExecutorService完成后,您可以通过将它们出列并关闭它们来关闭套接字.这确实增加了另一个的额外开销,BlockingQueue但如果你正在进行Socket通信,你将不会注意到它.
我们不相信ThreadFactory能满足我们的需求......
如果您使用线程本地,我认为您可以使这项工作.您的线程工厂将创建一个线程,首先打开套接字,将其存储在本地线程中,然后调用Runnablearg,它使用套接字完成所有工作,从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;
}
}));
Run Code Online (Sandbox Code Playgroud)
所以你的任务将实现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 ...
}
}
Run Code Online (Sandbox Code Playgroud)
我认为你应该使用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...
}
}
Run Code Online (Sandbox Code Playgroud)
每个线程都有自己的套接字.
| 归档时间: |
|
| 查看次数: |
7123 次 |
| 最近记录: |