public interface CBlockingQueue<E> { boolean add(E e); E take(); }
import java.util.concurrent.Delayed; import java.util.concurrent.FutureTask; import java.util.concurrent.RunnableScheduledFuture; import java.util.concurrent.TimeUnit; public class CScheduledThreadPoolExecutor extends CThreadPoolExecutor { public CScheduledThreadPoolExecutor() { super(new CDelayedWorkQueue()); } public void test(Runnable r, String result) { CScheduledFutureTask<String> sft = new CScheduledThreadPoolExecutor.CScheduledFutureTask<>(r, result); super.getQueue().add(sft); ensurePrestart(); } // 语法错误 static class CDelayedWorkQueue<Runnable> implements CBlockingQueue<Runnable> { static class CDelayedWorkQueue implements CBlockingQueue<Runnable> { private RunnableScheduledFuture<?>[] queue = new RunnableScheduledFuture<?>[5]; @Override public boolean add(Runnable r) { RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>) r; queue[0] = e; return true; } @Override public RunnableScheduledFuture take() { RunnableScheduledFuture<?> first = queue[0]; return first; } } private class CScheduledFutureTask<V> extends FutureTask<V> implements RunnableScheduledFuture<V> { CScheduledFutureTask(Runnable r, V result) { super(r, result); } @Override public boolean isPeriodic() { return false; } @Override public long getDelay(TimeUnit unit) { return 0; } @Override public int compareTo(Delayed o) { return 0; } } }
public class CThreadPoolExecutor { private final CBlockingQueue<Runnable> workQueue; public CThreadPoolExecutor(CBlockingQueue<Runnable> workQueue) { this.workQueue = workQueue; } public CBlockingQueue<Runnable> getQueue() { return workQueue; } void ensurePrestart() { addWorker(); } private boolean addWorker() { CWorker w = new CWorker(); w.run(); return true; } final void runWorker() { Runnable r = workQueue.take(); r.run(); } // 内部类 private final class CWorker { public void run() { runWorker(); } } }
public class ScheduledTest { public static void main(String[] args) { CScheduledThreadPoolExecutor dtpe = new CScheduledThreadPoolExecutor(); dtpe.test(() -> System.out.println("模拟CBlockingQueue泛型的类执行"), ""); } }
标签:return,RunnableScheduledFuture,CBlockingQueue,ScheduledThreadPoolExecutor,class, From: https://www.cnblogs.com/dzpykj/p/17482859.html