NorCal19
asked on
Multithreading Problem
I multithread a process for each record returned from the query.
The problem is that I call addToFinalLetter() as each job completes.
I actually need to make sure that addToFinalLetter() in the order the query returns the records so that they are in the correct order.
I'm having a hard time explaining what I mean, so if anybody has any idea what I'm talking about or questions.. I would appreciate any help.
Thanks!
Tom
public void run()
{
try
{
workers = new ArrayList();
for ( int i = 0; i < simulWorkers; i++ )
{
workers.add( i, new Worker( getNextLogSeqno(), printSeqno, dbName, imageDbName, userId ) );
((Worker) workers.get( i ) ).start();
}
while ( completed < letterCount )
{
for ( int i = 0; i < simulWorkers; i++ )
{
worker = ((Worker) workers.get( i ) );
if ( worker.isDone() )
{
worker.done = false;
addToFinalLetter( worker.getLetter() );
completed++;
logSeqno = getNextLogSeqno();
if ( logSeqno != 0 )
{
workers.set( i, new Worker( logSeqno, printSeqno, dbName, imageDbName, userId ) );
((Worker) workers.get( i ) ).start();
}
}
}
}
workers = null;
done = true;
}
catch ( Exception ignore ) {}
}
The problem is that I call addToFinalLetter() as each job completes.
I actually need to make sure that addToFinalLetter() in the order the query returns the records so that they are in the correct order.
I'm having a hard time explaining what I mean, so if anybody has any idea what I'm talking about or questions.. I would appreciate any help.
Thanks!
Tom
public void run()
{
try
{
workers = new ArrayList();
for ( int i = 0; i < simulWorkers; i++ )
{
workers.add( i, new Worker( getNextLogSeqno(), printSeqno, dbName, imageDbName, userId ) );
((Worker) workers.get( i ) ).start();
}
while ( completed < letterCount )
{
for ( int i = 0; i < simulWorkers; i++ )
{
worker = ((Worker) workers.get( i ) );
if ( worker.isDone() )
{
worker.done = false;
addToFinalLetter( worker.getLetter() );
completed++;
logSeqno = getNextLogSeqno();
if ( logSeqno != 0 )
{
workers.set( i, new Worker( logSeqno, printSeqno, dbName, imageDbName, userId ) );
((Worker) workers.get( i ) ).start();
}
}
}
}
workers = null;
done = true;
}
catch ( Exception ignore ) {}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I actually ended up doing it in a really ghetto way:
I first loop through the query and load the logSeqnos into an ArrayList I call sequence. This will be the order I must append them later.
Then, I also have a HashMap called dataPairs that is populated as each worker completes. The key for the HashMap is the log seqno and the value is the object that I pass to the append method.
So once all workers have completed I can run the following:
private void createDocument()
{
try
{
for ( int i = 0; i <= sequence.size(); i++ )
{
finalLetter.appendDocument ((Letter) dataPairs.get( sequence.get( i ) ) );
}
}
catch ( Exception ignore ) {}
}
Thank you both for your help.
I will split the points evenly
I first loop through the query and load the logSeqnos into an ArrayList I call sequence. This will be the order I must append them later.
Then, I also have a HashMap called dataPairs that is populated as each worker completes. The key for the HashMap is the log seqno and the value is the object that I pass to the append method.
So once all workers have completed I can run the following:
private void createDocument()
{
try
{
for ( int i = 0; i <= sequence.size(); i++ )
{
finalLetter.appendDocument
}
}
catch ( Exception ignore ) {}
}
Thank you both for your help.
I will split the points evenly
:-)
ASKER
I tried briefly to use the wait and notify but I kept getting some ObjectMonitorException....
Thanks,
Tom