Java Queue Usage loadBalancingLeftHeavy(List tasks, int size)

Here you can find the source of loadBalancingLeftHeavy(List tasks, int size)

Description

load Balancing Left Heavy

License

Open Source License

Declaration

public static <T> List<T> loadBalancingLeftHeavy(List<T> tasks, int size) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Main {
    public static <T> List<T> loadBalancingLeftHeavy(List<T> tasks, int size) {
        Queue<T> sucQueue = new LinkedList<T>();
        sucQueue.addAll(tasks);//from  ww  w  .  jav  a 2 s.  c  om
        int todo_size = sucQueue.size();

        List<List<T>> pList = new ArrayList<List<T>>(size);
        for (int i = 0; i < size; i++)
            pList.add(new LinkedList<T>());

        T t1;
        int b = 0;
        for (int i = 0; i < todo_size / size; i++) {
            for (int j = 0; j < size; j++) {
                t1 = sucQueue.poll();
                pList.get(Math.abs(b * (size - 1) - j)).add(t1);
            }
            b = (b == 0 ? 1 : 0);
        }

        for (int j = 0; j < size && !sucQueue.isEmpty(); j++) {
            t1 = sucQueue.poll();
            pList.get(j).add(t1);
        }

        List<T> result = new ArrayList<T>(todo_size);
        for (int i = 0; i < size; i++)
            for (T t2 : pList.get(i))
                result.add(t2);

        return result;
    }
}

Related

  1. elementFail(Queue q)
  2. get(Queue queue, int index)
  3. getAllElementsFromQueueAsList(Queue queue)
  4. isListBased(Class type)
  5. isQueue(final Object value, final boolean acceptNull)
  6. offerUntilSuccess(T entry, Queue queue)
  7. parsePathToStack(String path)
  8. pollWhileNotEmpty(Queue queue)
  9. queue(final Queue queue, final Object o, final Class clazz, final boolean removeNull)

  10. HOME | Copyright © www.java2s.com 2016