Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads in jsp programs  RSS feed

 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp program which is served by the tomcat jsp container. The jsp file includes(imports) a SearchClient.java class. The class has a search() method. The search method spawns two new Threads(searchthread - inner class in searchclient extends thread) these new threads contact two servers which respond with some data(results). These results are then merged in the search() method and returned to the jsp program which displays the results.

The problem is the jsp program does not wait for the threads to complete execution though i use join method on them.
One of my frends says that the jsp program, which is essentially run as a thread by the server(tomcat) does not have permissions to spawn new threads. Is this true? if yes what should I do? Do I have to go for beans? If I use beans can I reuse the current code?
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're willing to take the performance hit of running the searches in order, you could consider calling run() directly instead of start().
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Dave, but what if I want the threads in my program. How do I go it?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share some of the code that starts and joins threads? I'd expect this to work. I know the EJB container "forbids" applications making new threads but I don't think the servlet spec does.
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
I'm posting some of the code. The results.jsp instantiates the SearchClient class and calls the search() method which returns an arraylist. The search method spawns two new threads which communicate with two local servers. The communication protocol between the thread and the local servers is a message (server) - ack(client) protocol.
The threads are getting created and the communication with servers is also established. The local servers return one object at a time from which the thread builds an array, merges the two arrays from the two servers into an array list and returns it. For every object from server, the thread has to send an ack.
My problem is the thread is not sending the ack to the server. So the servers block after sending the first object, meanwhile the jsp code doesnt wait and continue with it execution.
Here is the code...pls dont mind the indentation
/** results.jsp */
<%@ page import = "javax.servlet.*, javax.servlet.http.*, java.io.*,java.util.ArrayList, com.URLMap, com.SDoc, com.SearchClient "%>
<%@ page isThreadSafe="true"%>
<%@include file="index.jsp"%>

<%
if(URLMap.size()==0){
URLMap.buildMap();
}
String query=request.getParameter("query");
String context = request.getParameter("context");
String lastScore = "0";
if(lastScore==null)
lastScore="0";
SearchClient sc = new SearchClient(query,context,lastScore);
ArrayList<SDoc> results = new ArrayList<SDoc>();
results = sc.search();

%>
/** results.jsp */

/** SearchClient.search() */

public ArrayList<SDoc> search(){
ArrayList<SDoc> results = new ArrayList<SDoc>();
try{

SearchThread st1 = new SearchThread(localServers[0],qry,ct);
SearchThread st2 = new SearchThread(localServers[1],qry,ct);
st1.run();
st2.run();
System.out.println("local server threads started");
//st2.join();
//st1.join();
//System.out.println("results are " + doc128.get(1).getScore() + doc87.size());
int i=0,j=0,k=0;
........ more code here
/** SearchClient.search() */

/** SearchThread.run */
public synchronized void run(){
//System.out.println("inside SearchThread.run");
try{
//sleep(100);
int newControlPort=0;
int newDataPort=0;
inLine="nothing";
outLine="";
client = new Socket(ip,4444);
out= new PrintWriter(client.getOutputStream(),true);
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("initialized in and out on" + client.getPort() + client.getLocalSocketAddress());
while(!in.ready()){
//System.out.println("waiting for server ready");
yield();
}
new ControlPort=Integer.parseInt(in.readLine().trim());
if(newControlPort!=0){
System.out.println("new control port is " + newControlPort);
out.close();
in.close();
client.close();
try{
controlClient = new Socket(ip,newControlPort);
System.out.println("new control port is " + controlClient.getPort());
}catch(IOException ie){
ie.printStackTrace();
}

/** SearchThread.run */
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really sorry Stan but in the code i posted the join methods are commented i forgot to remove them. they are included in the code
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!