ThreadPoolExecutor 例子

1. 重写RejectedExecutionHandler

线程池在BlockingQueue用完的情况下,会执行这里。可以利用这个方法把数据存下来。等空闲的时候在运行。

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
public class UserRejectHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {
        System.out.println("task rejected. " + threadPoolExecutor.toString());
    }
}


2. 创建ThreadFactory 。生产Thread

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
// UserThreadFactory.java
public class UserThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger nextId = new AtomicInteger(1);
    UserThreadFactory(String whatFeatureOfGroup) {
        this.namePrefix = "UserThreadFactory's " + whatFeatureOfGroup + "-Worker-";
    }

    public Thread newThread(Runnable runnable) {
        String name = this.namePrefix + nextId.getAndIncrement();
        Thread thread = new Thread(null, runnable, name, 0);
        System.out.println(thread.getName());
        return thread;
    }
}
class Task implements Runnable {
    private final AtomicLong count = new AtomicLong(0L);
    public void run() {
        System.out.println("Starting to run running_" + count.getAndIncrement());
        try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        System.out.println("End to run running_" + count.getAndIncrement());
    }
}


3. 执行15个任务。

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class UserThreadPool {
    public static void main(String[] args) {
        BlockingDeque blockingDeque = new LinkedBlockingDeque<>(13);
        UserThreadFactory f1 = new UserThreadFactory("UserThreadFactory 1");
        UserRejectHandler handler = new UserRejectHandler();
        ThreadPoolExecutor threadPoolFirst = new ThreadPoolExecutor(1, 2,
                60, TimeUnit.SECONDS, blockingDeque, f1, handler);
        Runnable task = new Task();
        for (int i = 0; i < 15; i++) {
            threadPoolFirst.execute(task);
            System.out.println("threadPoolFirst.execute.."+i);
        }   
    }
}

执行结果

UserThreadFactory's UserThreadFactory 1-Worker-1
threadPoolFirst.execute..0
threadPoolFirst.execute..1
threadPoolFirst.execute..2
Starting to run running_0
threadPoolFirst.execute..3
threadPoolFirst.execute..4
threadPoolFirst.execute..5
threadPoolFirst.execute..6
threadPoolFirst.execute..7
threadPoolFirst.execute..8
threadPoolFirst.execute..9
threadPoolFirst.execute..10
threadPoolFirst.execute..11
threadPoolFirst.execute..12
threadPoolFirst.execute..13
UserThreadFactory's UserThreadFactory 1-Worker-2
threadPoolFirst.execute..14
Starting to run running_1
End to run running_2
End to run running_3
Starting to run running_4
Starting to run running_5
End to run running_6
End to run running_7
Starting to run running_8
Starting to run running_9
End to run running_11
End to run running_10
Starting to run running_12
Starting to run running_13
End to run running_14
End to run running_15
Starting to run running_16
Starting to run running_17
End to run running_18
Starting to run running_20
End to run running_19
Starting to run running_21
End to run running_22
End to run running_23
Starting to run running_24
Starting to run running_25
End to run running_26
Starting to run running_27
End to run running_28
End to run running_29
 

你可能感兴趣的:(Java,java,开发语言,后端)