• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem using thread pool in servlet, answering to a client page  RSS feed

 
Susana Santos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an html page with 3 input fields, when the user clicks the submit button, a jquery ajax get request is made to a servlet with the information of the 3 fields.
When the requests arrives in the server(my servlet), I need to create a thread for each input type, and manage this thread pool, put them in order.
For example if thread 1 finishes earlier, then thread 2 should print the response to the client, before the next thread.
I'm using a sleep method in each thread with a random sleep time, to determine the time each thread should take.

On the client side, in the html, I have div, or a list, whatever, to show the server response.

To send the information to the client, I just need to make PrintWriter.write on the servlet response.
The problem that I'm having is that the information of each thread appears at client side at the same time.
The output only appears on client side after all the threads have finished.

For example , I have thread a which took 200ms, a thread b which took 300ms, and a thread c which took 100ms.
The result on client side will only appear after 300ms, for example the result would be the string "cab"

So I have this javascript function in my html page

$.ajax({
type : 'get',
url : 'ServletResponse2',
data : {
"name1" : name1,"name2" : name2, "name3" : name3
},
success : function(xhr,data) {
console.log("xhr:"+xhr);
console.log("xhr text:"+xhr.responseText);
console.log("data:"+data);
console.log("data2:"+data.data);
$("#listResponse2").append("<li>" + xhr);
$("#listResponse2").append("</li>");
},
complete : function(xhr,data) {
response = xhr.responseText;
console.log("exception:"+response);
$("#listResponse").append("<li>" + response);
$("#listResponse").append("</li>");

},

error : function(e) {
console.log("exception:"+e);
}

});

Then in the servlet I have this get method calling a executor service, which calls a Class Worker Thread which implements runnable

protected void doGet(HttpServletRequest request,HttpServletResponse response)
{
ExecutorService executor= Executors.newFixedThreadPool(3);
PrintWriter out=null;
response.setCharacterEncoding("UTF-8");
String name1 = request.getParameter("name1");
String name2 = request.getParameter("name2");
String name3 = request.getParameter("name3");
String name="";

for (int i = 1; i < 4; i++) {
name=request.getParameter("name"+i);
try {
out = response.getWriter();
Runnable worker = new WorkerThread("" + name,response);
executor.execute(worker);
} catch (IOException e) {

e.printStackTrace();

}
}

executor.shutdown();
while (!executor.isTerminated()) {
}

System.out.println("Finished all threads");

out.print(name);

}

In the WorkerThread.java, I creat the run method where I'm writing the response for each servlet, I even put the out.flush and response.flushbuffer

public class WorkerThread implements Runnable {

private String command;
PrintWriter out;
HttpServletResponse response;


public WorkerThread(String s,HttpServletResponse sresponse){
this.command=s;
this.response=sresponse;
}

@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
try {
out = response.getWriter();

out.write(command);
out.flush();
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}

processCommand();
System.out.println(Thread.currentThread().getName()+" End.");
}

private void processCommand() {
try {
Long time=(long)(Math.random() * 5000);
Thread.sleep(time);
System.out.println("Thread.getName():"+Thread.currentThread().getName()+"|comand:"+command+"|time:"+ time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public String toString(){
return this.command;
}
}

I also tried to use a Callabe in the executor service instead of a runnable, but the result was the same.
Can anyone tell me what am I doing wrong, why the result of the threads is arriving at the same time to the client?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you spawning your own threads in the first place?
 
Susana Santos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is to simulate when on the client side instead of doing several ajax requests,only do one with a list and that then have to separate them on server side. This part its a requirement of the exercise,was imposed to me.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!