Results 1 to 10 of 10
  1. #1
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default 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 .

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default 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();
    		}
    		
    	}
    
    
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.

    ** This space for rent **

  5. #5
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default 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 ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: issues in threading

    I will try .

  8. #8
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default 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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.

    ** This space for rent **

  10. #10
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: issues in threading

    oh great .. And thanks for helping me

Similar Threads

  1. Threading
    By kaptan_singh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2011, 08:23 PM
  2. threading help
    By the reporter in forum AWT / Swing
    Replies: 19
    Last Post: 06-21-2010, 05:50 PM
  3. Threading
    By hedonist in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2010, 02:21 PM
  4. need some help with threading
    By dinosoep in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 05:31 PM
  5. Threading in EJB
    By java08 in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •