?
Solved

Multithreading Problem

Posted on 2004-08-06
5
Medium Priority
?
196 Views
Last Modified: 2010-03-31
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 ) {}
  }

0
Comment
Question by:NorCal19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 435 total points
ID: 11739060
You should probably use wait/notify to synchronize on the sequence number. Make the thread wait if it's not got the right sequence number. If a thread is allowed access, it should call notify after writing
0
 

Author Comment

by:NorCal19
ID: 11739150
Can you please show me an example of how this would work?

I tried briefly to use the wait and notify but I kept getting some ObjectMonitorException....

Thanks,

Tom
0
 
LVL 7

Accepted Solution

by:
JugglerW earned 435 total points
ID: 11739223
Mixture of Java and Pseudocode, but should give you an idea:

Integer seqNumber = new Integer(0);

while ( !worker.isDone() )
{
    if ( worker.getSeqNumber().equals( seqNumber ) )
    {
        addToFinalLetter( worker.getLetter() );
        increment seqNumber;
        seqNumber.notifiyAll(); // let other workers try to finish
    }
    else
            seqNumber.wait(); // wait till my number will come
}

0
 

Author Comment

by:NorCal19
ID: 11739465
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11739529
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question