I Have This Workers List:
private List<Worker> buildWorkerList( Producer producer ) {
List<Worker> workerList = new ArrayList<Worker>( 11 );
workerList.add( new WorkerImpl( "Garnett", producer ) );
workerList.add( new WorkerImpl( "Durant", producer ) );
workerList.add( new WorkerImpl( "Xavier", producer ) );
workerList.add( new WorkerImpl( "Daniel", producer ) );
workerList.add( new WorkerImpl( "Leila", producer ) );
workerList.add( new WorkerImpl( "Jimmy", producer ) );
workerList.add( new WorkerImpl( "Monch", producer ) );
workerList.add( new WorkerImpl( "Ronald", producer ) );
workerList.add( new WorkerImpl( "Ascah", producer ) );
workerList.add( new WorkerImpl( "Guru", producer ) );
return workerList;
}
I need to have my producer allocate 100 jobs to these workers in such a way that a max of 3 jobs are given at a time and in such a way as job1 job2 job3 are to Garnett, job4 job5 job6 to Durant, job7 job8 job9 to Xavier......
The workers are then started after assigning the first three jobs to each worker.Every 4 seconds the job allocator loops through all the workers and makes sure they all have at least 3 jobs each.The job allocator keeps allocating jobs to the consumers until it has
given out 100 jobs.
Now here is my assign method in the ProducerImpl class:
public synchronized void assign( Job job ) {
Set<Worker> workerSet = jobMap.keySet();
LinkedBlockingDeque<Job> jobQueue;
StringBuffer sb;
for ( Worker worker : workerSet ) {
jobQueue = jobMap.get( worker );
sb = new StringBuffer();
sb.append( "Assigning job " );
sb.append( job.getJobNumber() );
sb.append( " to " );
sb.append( worker );
sb.append( "'s jobs Deque" );
Logger.debug( "Producer : " + sb.toString() );
if ( ! jobQueue.offerFirst( job ) ) {
jobQueue.pollLast();
jobQueue.offerFirst( job );
}
}
}
The problem with This code is that it iterates and assigns job1 to all the ten workers then job2 to all the ten and so on. How Do I single out Garnett Alone and assign job1 job2 job3 single out Durant assign job4 job5 job6 and so on. You realise I have WorkerImpl and JobImpl classes too, I can post them up too if this info is not sufficient. I am running the class that contains the WorkersList code snippet.Should I introduce a Singletone and ExecutorService??