multithreading - How to make an optimized scenario for Java concurrent application? -


i've been looking similar questions these, requirements think need special, explain in detail.

first of need migrate system used work way:

  1. a class called serverpool(thread) initializes main class.
  2. this serverpool creates queue receive sockets , vector manage worker threads (s.

so in code pool, have following:

public class serverpool extends thread {     private linkedblockingqueue<searchquery> workqueue; //searchquery class defined can handle 2 type of processes (for sockets , single strings)     private vector<searchthread> workers;     private final int nthreads = 10;     private int typeofquery;      public serverpool() {     workers = new vector<searchthread>(num_thread);     workqueue = new linkedblockingqueue<searchquery>();     this.typeofquery = typeofquery;     searchthread search = new searchthread(workqueue);     search.start();     workers.add(search); }  public void run() {     while(true){     searchquery client = null;      if (typeofquery == 1) {         client = new socketquery(....);         } else if (typeofquery == 2) {         client = new stringquery(...);         }         workqueue.put(client); } } 

for searchthread executes process:

public class searchthread extends thread {     private linkedblockingqueue<searchquery> workqueue = null;     private searchquery request = null;      public searchthread(linkedblockingqueue<searchquery> worksource) {         workqueue = worksource;     }         public void run() {         request = workqueue.take();         //here process request         //and use printwriter give "response"                 }  } 

this used work using telnet sockets, i've been asked convert web service, web service supposed return value, think of using callable, future , thread pools, can't replicate same behavior, tried implementing this:

public class newserverpool {      private final int nthreads = 10;     private executorservice executor;     private linkedblockingqueue<searchquery> workqueue;     private vector<future<string>> futures;     private boolean end = true;      public newserverpool(int port, searchquery typeofquery) {         executor = executors.newfixedthreadpool(nthreads);         workqueue = new linkedblockingqueue<searchquery>();         futures = new vector<future<string>>();      }   } 

and search thread callable

public class newsearchthread implements callable<string>{      private searchquery searchquery;      public newsearchthread(searchquery searchquery) {         this.searchquery = searchquery;     }      @override     public string call() throws exception {         string xmlresponse = null;          if (searchquery == null) {             throw new invalidsearchqueryexception("the search query not valid or has null value: " + searchquery);         }          if (searchquery instanceof sockettimed) {             system.out.println("it socket timed query type");         } else if (searchquery instanceof webservicequery) {             system.out.println("it web service query type");         }          xmlresponse = searchquery.manageresponse();          return xmlresponse;     } 

so i've got stucked in server pool, asumming webservice invoke new instance of server pool (newserverpool) in case, how continue this? please grateful if can me. in advance, best regards.

a couple of things:

first off, original serverpool class flawed, in ever instantiates 1 instance of searchthread. think meant start nthreads (10) searchthreads.

next, looks you've changed approach of newsearchthread searchthread - in constructor newsearchthread takes searchquery argument, whereas searchthread takes searchquery off of blockingqueue.

and newserverpool class differs in approach serverpool, in serverpool's run() method continuously places new searchquerys blockingqueue. in contrast, newserverpool's constructor takes single searchquery , nothing it.

how started:

public class newserverpool extends thread {      private final int nthreads = 10;     private executorservice executor;     private vector<future<string>> futures;      public newserverpool(int port, searchquery typeofquery) {         executor = executors.newfixedthreadpool(nthreads);         futures = new vector<future<string>>();     }      public void run() {         while(true){             searchquery client = null;              if (typeofquery == 1) {                 client = new socketquery(....);             } else if (typeofquery == 2) {                 client = new stringquery(...);             }             futures.add(executor.submit(new newsearchthread(client)));         }     } } 

note "to started"... above still needs additions such proper exiting of run() method when it's time stop fielding requests (but that's topic).


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -