in Education by
In my Java application I want to have a pool of threads (I use ExecutorService for this) that connect to a memcached server to issue some requests. I would like that each Thread has its own connection that is opened once and should be kept during the whole running time. I was planning to use the following code that I found in another question: Executor Service ExecutorService threadPool = Executors.newFixedThreadPool(5, Executors.defaultThreadFactory()); Runnables executed by the Threads in the pool public class SocketTask implements Runnable { private String workDetails; private static final ThreadLocal threadLocal = new ThreadLocal(){ @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(); } } My question is, when I initialize the socket how can I do it so I am able to pass an argument (say a String with the IP of the memcached server) to the new Socket() call? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Example with a factory instead of static fields. Factory creation: final SocketTaskFactory socketTaskFactory = new SocketTaskFactory("...", 12345); Factory use (instead of now "new SocketTask(...)"): Runnable socketTask = socketTaskFactory.createSocketTask("the work details"); Factory code, including the socket task as an inner class: package threadlocal; import java.io.IOException; import java.net.Socket; public class SocketTaskFactory { final String host; final int port; public SocketTaskFactory(String host, int port) { this.host = host; this.port = port; } public Runnable createSocketTask(String workDetails) { return new SocketTask(workDetails); } final ThreadLocal socketForThread = new ThreadLocal() { @Override protected Socket initialValue() { try { return new Socket(host, port); } catch(IOException exception) { throw new RuntimeException(exception); } } }; private class SocketTask implements Runnable { private final String workDetails; public SocketTask(String workDetails) { this.workDetails = workDetails; } @Override public void run() { Socket socket = socketForThread.get(); // ... } } }

Related questions

0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked May 27, 2022 in Education by JackTerrance
0 votes
    We have a service method GetDataParallel( ) which maybe called by many clients currently, and we use the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I'm trying to make sure that all my threads of Class A have finished before notify is called. At ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    _________ require you to pass a function whose argument is a vector of parameters. (a) optimize() (b) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    In C, how can i pass more than one argument to a thread? Normally, I do it in a way like, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    How do we pass command line argument in Eclipse? (a) Arguments tab (b) Variable tab (c) Cannot ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    How is a bare repository different from the standard way of initializing a Git repository?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    I want to pass a parameter into an XPath expression. (//a/b/c[x=?],myParamForXAttribute) Can I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    AAE's "PDF Integration" command allows you to split a .pdf into a separate single file for a selected number ... by the hyphen. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    There is no explanation in python documentation whether parameters are passed by value or reference and why the ... actual reference? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...