public boolean waitForAll( Collection workItems,
long timeout )
{
Iterator workItemsIterator = workItems.iterator( );
Object current;
WorkItem workItem;
int status;
if ( timeout == WorkManager.IMMEDIATE )
{
while ( workItemsIterator.hasNext( ) )
{
current = workItemsIterator.next( );
if ( current instanceof WorkItem )
{
status = ( (WorkItem) current ).getStatus( );
if ( ( status != WorkEvent.WORK_COMPLETED ) && ( status != WorkEvent.WORK_REJECTED ) )
{
return false;
}
}
}
}
else if ( timeout == WorkManager.INDEFINITE )
{
while ( workItemsIterator.hasNext( ) )
{
current = workItemsIterator.next( );
if ( current instanceof WorkItem )
{
workItem = (WorkItem) current;
synchronized ( workItem )
{
status = workItem.getStatus( );
while ( ( status != WorkEvent.WORK_COMPLETED ) && ( status != WorkEvent.WORK_REJECTED ) )
{
try
{
workItem.wait( );
}
catch ( InterruptedException e )
{
// not sure if this is the right thing to do
return false;
}
status = workItem.getStatus( );
}
}
}
}
}
else
{
long absTimeout = System.currentTimeMillis( ) + timeout;
while ( workItemsIterator.hasNext( ) )
{
current = workItemsIterator.next( );
if ( current instanceof WorkItem )
{
workItem = (WorkItem) current;
synchronized ( workItem )
{
status = workItem.getStatus( );
while ( ( status != WorkEvent.WORK_COMPLETED ) && ( status != WorkEvent.WORK_REJECTED ) )
{
if ( timeout > 0 )
{
try
{
workItem.wait( timeout );
}
catch ( InterruptedException e )
{
// not sure if this is the right thing to do
return false;
}
timeout = absTimeout - System.currentTimeMillis( );
}
else
{
return false;
}
status = workItem.getStatus( );
}
}
}
}
}