Solved

Multithreading Problem

Posted on 2004-08-06
5
191 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
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 145 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 145 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 86
noX challenge 17 76
nestparen challenge 4 56
strCopies  challenge 17 73
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now