Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti threading with Java Executor
    primarykey
    data
    text
    <p>I am stuck with this following problem. Say, I have a request which has 1000 items, and I would like to utilize Java Executor to resolve this.</p> <p>Here is the main method</p> <pre><code>public static void main(String[] args) { //Assume that I have request object that contain arrayList of names //and VectorList is container for each request result ExecutorService threadExecutor = Executors.newFixedThreadPool(3); Vector&lt;Result&gt; vectorList = new Vector&lt;Result(); for (int i=0;i&lt;request.size();i++) { threadExecutor.execute(new QueryTask(request.get(i).getNames, vectorList) } threadExecutor.shutdown(); response.setResult(vectorList) } </code></pre> <p>And here is the QueryTask class</p> <pre><code>public QueryTask() implements Runnable { private String names; private Vector&lt;Result&gt; vectorList; public QueryTask(String names, Vector&lt;Result&gt; vectorList) { this.names = names; this.vectorList = vectorList; } public void run() { // do something with names, for example, query database Result result = process names; //add result to vectorList vectorList.add(result); } } </code></pre> <p>So, based on the example above, I want to make thread pool for each data I have in the request, run it simultaneously, and add result to VectorList. And at the end of the process, I want to have all the result already in the Vector list.</p> <p>I keep getting inconsistent result in the response. For example, if I pass request with 10 names, I am getting back only 3 or 4, or sometimes nothing in the response. I was expecting if I pass 10, then I will get 10 back.</p> <p>Does anyone know whats causing the problem?</p> <p>Any help will be appreciate it.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload