자바 다 중 스 레 드 통합 관리

4718 단어
LocalExecutor. java 는 자신의 스 레 드 탱크 실 체 를 정의 합 니 다.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LocalExecutor {
    private String name;

    private int poolSize;

    private int blockQueueWarningSize;

    private ExecutorService executorService = null;

    public LocalExecutor(String name, int poolSize, int blockQueueWarningSize) {
        this.name = name;
        this.poolSize = poolSize;
        this.blockQueueWarningSize = blockQueueWarningSize;
        this.executorService = Executors.newFixedThreadPool(poolSize);
    }

    public String getName() {
        return name;
    }

    public int getPoolSize() {
        return poolSize;
    }

    public int getBlockQueueWarningSize() {
        return blockQueueWarningSize;
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

}

LocalExecutor Manager. java 는 스 레 드 탱크 를 집중 적 으로 관리 하고 모니터링 스 레 드 를 시작 합 니 다.모니터링 간격 은 5 초 에 한 번 입 니 다.필요 하 다 면 스스로 고 칠 수 있다.
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class LocalExecutorManager {
    private static ConcurrentHashMap hashMap = null;

    private LocalExecutorManager() {
    };

    private static void init() {

        hashMap = new ConcurrentHashMap<>();
        hashMap.put("processlog", new LocalExecutor("processlog", 5, 10));
        // add other thread
        // hashMap.put("processCompareData", new LocalExecutor("processCompareData", 12, 100));

        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(new ExecutorMonitor(hashMap), 1, 5, TimeUnit.SECONDS);
    }

    public static ExecutorService getExecutorService(String name) {
        if (null == hashMap) {
            init();
        }
        if (null != hashMap && hashMap.containsKey(name)) {
            return hashMap.get(name).getExecutorService();
        }
        return null;
    }
}

ExecutorMonitor. java 스 레 드 모니터링
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

public class ExecutorMonitor implements Runnable {
    private ConcurrentHashMap hashMap = null;

    public ExecutorMonitor(ConcurrentHashMap hashMap) {
        this.hashMap = hashMap;
    }

    @Override
    public void run() {
        for (ConcurrentHashMap.Entry items : hashMap.entrySet()) {
            LocalExecutor executor = items.getValue();
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor.getExecutorService();
            if (threadPoolExecutor.getQueue().size() > executor.getBlockQueueWarningSize()) {
                System.out.println("Thread name:" + executor.getName() + ",  Thread max:" + executor.getPoolSize() + ", Thread queue:"
                        + threadPoolExecutor.getQueue().size() + ", Exceed warning thread:" + executor.getBlockQueueWarningSize());
            }
        }
    }
}

TestLocalExecutormanager. java 테스트
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;

import org.junit.Test;

public class TestLocalExecutormanager {
    @Test
    public void test() throws InterruptedException {
        ExecutorService executor = LocalExecutorManager.getExecutorService("processlog");
        int maxNum = 100;
        CountDownLatch latch = new CountDownLatch(maxNum);
        for (int i = 0; i < maxNum; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    System.out.println(Thread.currentThread().getName());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        latch.countDown();
                    }
                }
            });
        }
        latch.await();
        executor.shutdown();
        // System.out.println("==shutdown==");
        // Thread.sleep(2 * 60 * 1000);
        System.out.println("==end==");
    }
}

다음으로 이동:http://www.6tie.net/p/783610.html

좋은 웹페이지 즐겨찾기