multithreading - Multithreaded Java worker with a size restricted resource pool -


i have 'worker' class, uses resource 'client'. there may number of threads, running 'worker' @ given time. 'client' not thread-safe, i'm using 'threadlocal' it. 'client' connects server , executes http 'request' worker feeds 'client'.

public class worker { // client not thread-safe !!!  private static threadlocal<client> client = new threadlocal<client>();  @override protected void onget(request req) {     handlerequest(req); }  private void handlerequest(request req) {     somerunnableexecutor(new runnable() {         @override         public void run() {             get_client().send_req(req);         }     }); }  private client get_client() {     client c = client.get();     if (c == null) {        c = new client();        client.set(c);     }     return c; } 

at current implementation (above), stripped down clarity, there many "active" 'clients' there running 'workers'.

this problem because server being exhausted.

what can fix 'worker'. have no access 'client', server or executor runs workers.

what want have queue of 'client'(s) , piece of synchronized code, in 'worker', takes 'client' off queue, if queue empty 'worker' should wait till there 1 in queue him take. put 'client' queue - synchronized well.

i want keep simple possible, possible minimum changes made code.

no new classes, no factories, data structure hold 'client'(s) , synchronization.

i bit puzzled how achieve generally, fact 'client' not thread-safe , have 'threadlocal'(ize) it. how put in queue?

private static queue<threadlocal<client>> queue =        new linkedlist<threadlocal<client>>(); 

also, how/where initialize queue, once, 5 clients?

please share thoughts.

you don't need threadlocal here, want have less clients workers. need in blockingqueue. notice! supposed client's send_req synchronous, if it's not - code needs changes in run() method

public class worker {      private static final int clients_number = 5;     private static final blockingqueue<client> queue = new linkedblockingqueue<>(clients_number);      static {         (int = 0; < clients_number; i++)             queue.put(new client());     }      @override     protected void onget(request req) {         handlerequest(req);     }      private void handlerequest(request req) {         somerunnableexecutor(new runnable() {             @override             public void run() {                 try {                     client client = takeclient();                     client.send_req(req);                     putclient(client);                 } catch (interruptedexception e) {                     thread.currentthread().interrupt();                 }             }         });     }      private client takeclient() throws interruptedexception {         return queue.take();     }      private void putclient(client client) throws interruptedexception {         queue.put(client);     } } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -