Solved

Multithreading Problem

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 59
pagenation logic how it is working in my code 1 71
mysql jsp example issue 32 74
Tomcat 9 + java 8 error while trying to deploy a war file 2 723
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

726 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