/*
 * Copyright 2017 ZJNU ACM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zjnu.acm.judge.service;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author zhanhb
 */
@Service
public class JudgePool {

    private ExecutorService executorService;
    @Autowired
    private JudgeService judger;

    @PostConstruct
    public void init() {
        final ThreadGroup group = new ThreadGroup("judge group");
        final AtomicInteger counter = new AtomicInteger();
        final ThreadFactory threadFactory = runnable -> new Thread(group, runnable, "judge thread " + counter.incrementAndGet());
        executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), threadFactory);
    }

    @PreDestroy
    public void destory() {
        executorService.shutdownNow();
    }

    public CompletableFuture<?> add(long id) {
        return judger.toCompletableFuture(executorService, id);
    }

    public CompletableFuture<?> addAll(long... id) {
        return CompletableFuture.allOf(Arrays.stream(id).mapToObj(this::add).toArray(CompletableFuture[]::new));
    }

}

@@ -811,29 +811,164 @@
 current.
-ThreadFactory
+LinkedBlockingQueue;%0Aimport java.util.concurrent.ThreadFactory;%0Aimport java.util.concurrent.ThreadPoolExecutor;%0Aimport java.util.concurrent.TimeUnit
 ;%0Aimport
@@ -1086,16 +1086,50 @@
 estroy;%0A
+import lombok.extern.slf4j.Slf4j;%0A
 import o
@@ -1269,16 +1269,23 @@
 Service%0A
+@Slf4j%0A
 public c
@@ -1707,55 +1707,23 @@
     
-executorService = Executors.newFixed
+int n
 Thread
-Pool(
+s = 
 Runt
@@ -1764,24 +1764,408 @@
 rs()
-, threadFactory)
+;%0A        executorService = new ThreadPoolExecutor(nThreads, nThreads,%0A                0L, TimeUnit.MILLISECONDS,%0A                new LinkedBlockingQueue%3C%3E(),%0A                threadFactory) %7B%0A%0A            @Override%0A            protected void afterExecute(Runnable r, Throwable t) %7B%0A                if (t != null) %7B%0A                    log.error(%22%7B%7D%22, r, t);%0A                %7D%0A            %7D%0A%0A        %7D
 ;%0A  

Add log if judge failed