Solved

Multithreading Problem

Posted on 2004-08-06
5
193 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
simple java question 3 56
servlet doXXX methods 3 51
expectj telnet failing 5 38
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

825 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