aspose file tools
The moose likes Beginning Java and the fly likes get the lightest, but me! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get the lightest, but me!" Watch "get the lightest, but me!" New topic

get the lightest, but me!

mari bo

Joined: Feb 10, 2011
Posts: 7
hi everybody,

I need some help to change the following code.
I have a system with some workers, let's say 3. They can exchange some stuff between them, like work to do.
If worker1 decide to send some stuff to do to another worker, it has to see which worker has the minimun load of work,
asking to a coordinator which has the following method:

NB. index corresponds to the Id of the worker which has the minimum load.

What is my problem: in this way it may happen that a worker, let's say worker1,
want to send some stuff to do to another worker with the minimum load.
In that moment, worker1 has the minimum, load, so worker1 will send to itself
the stuff to do, and it is not correct.
So my question is: how to change to code above, to choose the worker with a minimum load,
without taking into account the worker which is asking for it?

Thank you for your attention and for you replies!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40399
Go through it very carefully with a pencil and paper, writing down the values, and what will happen under particular circumstances. Write down things like "if minimum load is greater than actual load . . . " Work out what the best starting value is to achieve this condition. Is it 0?

Take no notice of the above, which is all mistaken; I misread your original post.

I can see nothing wrong with that method. The problem is that you are not using it elsewhere. You should write a similar method to find the most loaded worker. You should also find their actual loads, so you can pass half the difference from one to another. Make no change to that method, but create a balanceLoad(int lightWorkerIndex, int overworkedWorkerIndex, int amountToTransfer) method and call that.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193


You can easily exclude the worker that is sending the load to another. You need to just pass the index of that worker to your method getLightest(). Just create a new array of worker excluding the worker that is sending the load and apply the same logic on this new array. e.g. I pass 'w' that the index of the worker.

Do not wait to strike till the iron is hot; but make it hot by striking....
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40399
Welcome to the Ranch
I agree. Here's the link:
subject: get the lightest, but me!