File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes thread newbie help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "thread newbie help" Watch "thread newbie help" New topic
Author

thread newbie help

Robert Upshall
Greenhorn

Joined: Aug 23, 2001
Posts: 25
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

Joined: Feb 04, 2002
Posts: 134
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

Joined: Feb 04, 2002
Posts: 134
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.
 
IntelliJ Java IDE
 
subject: thread newbie help
 
Threads others viewed
Too many connections
To load a huge records from a DB to a JTable
NX: Control of rows in JTable
Calling a web service in a Thread
MaximumThroughput- MltiThreading - Need help
IntelliJ Java IDE