• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread newbie help  RSS feed

 
Robert Upshall
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my situation.
I have a database that I want to retrieve records from, the number of records could varry from 100-10000. What I want to do is display these records in a JList. I do not, for obvious reasons, want to load all 10000 records first, I would rather for example load 200 or 300 at a time and have the rest loading on a thread.
Is this possible? I haven't used threads before, all my work until now has been accessing 20 records at a time, max, so I could get away with loading all records at once.
It will be way to slow to attempt this application with my current approach, I think threads are the anwser, but I would appreciate any advice/tips on if/how I can approach this problem.
Thanks,
Robert Upshall
rupshall@psasoft.com
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
What you can try out is something like this :
class DisplayRecords
{
Enumeration rs;
boolean newRs;
public DisplayRecords()
{
//Code for database connection
}
public synchronized void getRecords(int noOfRecords)
{
//Code for returning records in Enum.
// if no more records found return null
// notify();
}
public synchronized void displayRecords()
{
while(rs != null)
{
if (newRs)
{
//Code for displaying records from rs
//wait();
}
}
}
}
Here both the methods i.e. getRecords() & displayRecords() can now be called from different threads, provided called on the same object. Hence it can be simulated as follows :
class MyRun
{
public static void main(String args[])
{
DisplayRecords dr = new DisplayRecords();
new GetThread(dr);
new DisplayThread(dr);
// can continue with your rest of the programme.
}
}
class GetThread extends Thread
{
DisplayRecords dr;
public GetThread(DisplayRecords dr)
{
this.dr = dr;
this.start();
}
public void run()
{
dr.getRecords();
}
}
class DisplayThread extends Thread
{
DisplayRecords dr;
public DisplayThread(DisplayRecords dr)
{
this.dr = dr;
this.start();
}
public void run()
{
dr.displayRecords();
}
}
I hope this helps you out.
Regards,
Mehul K. Sanghvi
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
Please read one more line in getRecords()
i.e. after getting the records and saving it to Enum. ....
newRs = true;
rest of the code remains the same.
Regards,
Mehul K. Sanghvi.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!