似乎不可能创建一个缓存的线程池,它可以创建的线程数限制.
以下是在标准Java库中实现静态Executors.newCachedThreadPool的方法:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
Run Code Online (Sandbox Code Playgroud)
因此,使用该模板继续创建固定大小的缓存线程池:
new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new SynchronusQueue<Runable>());
Run Code Online (Sandbox Code Playgroud)
现在,如果你使用它并提交3个任务,一切都会好的.提交任何进一步的任务将导致被拒绝的执行异常.
试试这个:
new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runable>());
Run Code Online (Sandbox Code Playgroud)
将导致所有线程按顺序执行.即,线程池永远不会有多个线程来处理您的任务.
这是ThreadPoolExecutor的execute方法中的错误?或者这可能是故意的?还是有其他方式?
编辑:我想要一些与缓存线程池完全相同的东西(它根据需要创建线程,然后在一些超时后杀死它们)但是它可以创建的线程数量受到限制,并且一旦有了它就能够继续排队其他任务达到了它的线程限制.根据sjlee的回应,这是不可能的.查看ThreadPoolExecutor的execute()方法确实是不可能的.我需要继承ThreadPoolExecutor并覆盖execute(),就像SwingWorker一样,但SwingWorker在其execute()中所做的是一个完整的hack.
java concurrency multithreading executorservice threadpoolexecutor
那么如何在Doctrine 2中指定用于给定实体的存储引擎呢?
我正在创建一个需要全文索引的表,只有MyISAM存储引擎支持MySQL中的全文索引.
作为一方:看起来Doctrine 2不支持开箱即用的全文索引?也不是全文搜索?那是对的吗?
我今天碰到了这个,我唯一能想到的是这是Java编译器中的一个错误.下面的代码编译,但肯定看起来不正确(因为testMethod在子节点中有一个differenet签名但覆盖了父节点)并且会在运行时抛出类转换异常.
public interface TestInterface<T> {
public List<String> testMethod(); // <-- List<String>
}
public class TestClass implements TestInterface {
@Override
public List<Integer> testMethod() { // <-- List<Integer> overriding List<String>!!
return Collections.singletonList(1);
}
}
Run Code Online (Sandbox Code Playgroud)
并使用以上结构:
public void test() {
TestInterface<Boolean> test = new TestClass();
List<String> strings = test.testMethod();
for (String s : strings) {
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
所有这些编译都很好,但是如果运行它会明显抛出类别转换异常.
如果您删除<T>从TestInterface,或填充T在该行中TestClass implements TestInterface<T>,则代码将不再编译,这是有道理的.Imo <T>应该对编译没有影响,testMethod因为它不参与该方法.也许添加<T>到TestInterface导致编译器键入 - 擦除方法签名,即使T不参与这些方法......?
有谁知道这里发生了什么?