Results 1 to 10 of 10
Thread: issues in threading
- 03-05-2012, 04:48 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
issues in threading
i have to contact a class which uses thread to process . The java class mainly contacts the database and gets me the result set. Say i will be contacting the java class which uses a thread for twice and the main thread needs to print the result returned by the java class. The main thread is the servlet . How to do this ? If i try to make the servlet to wait Exception occurs and if i try to retrive the result set in the servlet i am can get only the empty result set.
I am using a array of resultset in the java class. Suggest me a way how to process this. I need to obtain the result set from the java class in the main Thread that is in the servlet once the 2 threads have completed its execution .
- 03-05-2012, 04:56 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: issues in threading
We'd need to see some code otherwise we're guessing what's actually going on.
Please do not ask for code as refusal often offends.
- 03-05-2012, 05:19 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
Re: issues in threading
I thought we cant stop the main thread so i passed the values to the parent thread which in turn will create two sub threads . And here is the required code.
The below is the servlet which calls the data_exe class which in turn creates a thread for executing the PreparedStatements simultaneously
Java Code:package controller; import java.io.IOException; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.sql.ResultSet; import data_aggregator.data_exe; import data_aggregator.data_thread; /** * Servlet implementation class DataAggregators */ public class DataAggregators extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DataAggregators() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ResultSet[] rs = new ResultSet[4]; Integer noofsub = (Integer)session.getAttribute("noofsub"); PreparedStatement[] sub = (PreparedStatement [])session.getAttribute("subquery"); data_exe dte = new data_exe(sub,noofsub); dte.start(); rs = dte.getResult(); for(int i=0;i<noofsub;i++) { try { while(rs[i].next()) { System.out.println("Executing in ResultSet"); System.out.println(rs[i].getString(1)); System.out.println(rs[i].getString(2)); } } catch(Exception e) { e.printStackTrace(); } } } }Java Code:package data_aggregator; import java.sql.PreparedStatement; import java.sql.ResultSet; public class data_exe extends Thread { PreparedStatement[] sub = new PreparedStatement[4]; int noofsub; data_thread[] dt = new data_thread[4]; ResultSet[] rs1 = new ResultSet[4]; public data_exe(PreparedStatement[] ps,int i ) { sub = ps ; noofsub = i; } public ResultSet[] getResult() { return rs1; } public void run() { for(int i=0;i<noofsub;i++) { dt[i] = new data_thread(sub[i],i); dt[i].start(); } for(int i=0;i<noofsub;i++) { try { Thread.currentThread().wait(); System.out.println(Thread.currentThread().getName()); } catch(Exception e) { e.printStackTrace(); } rs1[i] = data_thread.rs[i]; try { while(rs1[i].next()) { System.out.println("into data aggregator"); System.out.println(rs1[i].getString(0)); System.out.println(rs1[i].getString(1)); } } catch(Exception e) { e.printStackTrace(); } } } }Java Code:package data_aggregator; import java.sql.PreparedStatement; import java.sql.ResultSet; public class data_thread extends Thread { private PreparedStatement[] ps = new PreparedStatement[4]; int i=0; public static ResultSet[] rs = new ResultSet[4]; public data_thread(PreparedStatement psi,int w) { ps[i] = psi; } public synchronized void run() { try { rs[i] = ps[i].executeQuery(); while(rs[i].next()) { System.out.println(rs[i].getString(1)); System.out.println(rs[i].getString(2)); } Thread.currentThread ( ).notify(); } catch(Exception e) { e.printStackTrace(); } } }
- 03-05-2012, 05:33 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: issues in threading
You will, whatever happpens, need to wait for the data_exe (need to sort out your naming, byt the way) thread to finish before you can get the results.
Try doing this outside of a servlet, since that'll be a lot easier to test. Just a simple standalone app.
Also, if you get exceptions you need to give them here in full.Please do not ask for code as refusal often offends.
- 03-05-2012, 05:37 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
Re: issues in threading
But its a part of my project. Should i build it as a seperate app and contact it ? If so then also i need to use a servlet to contact it rite ? or is there any mechanism to call it ?
- 03-05-2012, 05:48 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: issues in threading
No.
What I mean is write something that does this so you can get an idea of what needs doing in the servlet to get that working.
You don't need the overhead of a web server to try and do this threaded processing.
Once you have something that works then simply stick that code structure in your app.
Cut and paste...:)Please do not ask for code as refusal often offends.
- 03-05-2012, 05:52 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
Re: issues in threading
I will try .
- 03-06-2012, 08:11 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
Re: issues in threading
Atlast foound the solution. Use join method in thread to make the child threads execute one after the another and store the result in a vector. Once the child threads complete the execution retrive the resultset from the vector.
But i was able to find this one out as i first tried with a seperate app and then included in my project . Thanks Tolls for your advice.
- 03-06-2012, 09:34 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: issues in threading
I do it that way all the time.
If i have a specific problem like this I always write a scratch program to do just the thing I'm trying to figure out.
XML parsing is a frequent one for me...:)Please do not ask for code as refusal often offends.
- 03-06-2012, 02:17 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
Threading
By kaptan_singh in forum Threads and SynchronizationReplies: 1Last Post: 01-31-2011, 08:23 PM -
threading help
By the reporter in forum AWT / SwingReplies: 19Last Post: 06-21-2010, 05:50 PM -
Threading
By hedonist in forum Advanced JavaReplies: 3Last Post: 03-13-2010, 02:21 PM -
need some help with threading
By dinosoep in forum New To JavaReplies: 3Last Post: 12-03-2009, 05:31 PM -
Threading in EJB
By java08 in forum Advanced JavaReplies: 2Last Post: 08-12-2008, 11:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks