1. 程式人生 > 實用技巧 >Collections集合工具類排序

Collections集合工具類排序

1、構造方法

  • corePoolSize : 核心執行緒數
  • maximumPoolSize : 最大執行緒數量
  • keepAliveTime : 空閒存活時間
  • unit :時間單位
  • workQueue :阻塞佇列,用來儲存等待執行的任務。
    • ArrayBlockingQueue:陣列有界佇列,先進先出
    • DelayQueue : 無界阻塞佇列,延遲期滿才能提取元素
    • LinkedBlockingQueue:鏈式結構,先進先出
    • PriorityBlockingQueue:支援優先順序排序的無界佇列,可以自定義實現compareTo()方法指定元素排序。不能保證優先順序元素的順序
    • SynchronousQueue :容納單元素
    • BlockingDeque :雙端佇列,在不能夠插入元素的時候,阻塞插入。沒有元素抽取的時候,阻塞抽取。
    • LinkedBlockingDeque:是雙向連結串列實現的雙向併發阻塞佇列。該阻塞佇列同時支援FIFO和FILO兩種操作方式,即可以從佇列的頭和尾同時操作(插入/刪除);並且,該阻塞佇列是支援執行緒安全。此外,LinkedBlockingDeque還是可選容量的(防止過度膨脹),即可以指定佇列的容量。如果不指定,預設容量大小等於Integer.MAX_VALUE
  • threadFactory:執行緒工廠,用來建立執行緒
  • handler :飽和策略
    • CallerRunsPolicy:用呼叫者所在的執行緒來執行任務
    • AbortPolicy :預設策略,直接丟擲異常
    • DiscardPolicy :直接丟棄任務
    • DiscardOldestPolicy:丟棄佇列最前的任務,並執行當前任務

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue
<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.acc = System.getSecurityManager() == null ? null : AccessController.getContext(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }

2、execute方法

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }