我非常困惑为什么以下演员不起作用:
ScheduledThreadPoolExecutor timeoutControl = (ScheduledThreadPoolExecutor) Executors.newSingleThreadScheduledExecutor();
Run Code Online (Sandbox Code Playgroud)
ScheduledThreadPoolExecutor 实现 ScheduledExecutorService。如果我不能在实际的类中使用它,那么这个 Executors 调用有什么意义呢?
我使用它是否错误(可能),有人可以提供一些指导吗?
是否有理由喜欢这样做:
private static ExecutorService service = Executors.newScheduledThreadPool(10);
Run Code Online (Sandbox Code Playgroud)
对此:
private static ExecutorService service = new ScheduledThreadPoolExecutor(10);
Run Code Online (Sandbox Code Playgroud) 这应该是任何Java Master的简单方法.我是一个新手只是想确认一件事.
我有一个实现Runnable的类,和许多这样的类一样,它的run()方法有一个无限循环.我想做一些任务,然后睡一会儿然后再回来.
如果在线程处于休眠状态时遇到中断异常会发生什么?
我认为会发生的是线程被暂停,现在无限循环无助于保持线程运行.我想确认一下我的理解是否正确.
如果发生了这种情况,那么再次启动线程的可行解决方案是什么?
每个方法的注释public static ExecutorService newCachedThreadPool()中的Executor类:
Threads that have not been used for sixty seconds are terminated and
removed from the **cache**.
Run Code Online (Sandbox Code Playgroud)
我想知道缓存在哪里以及它是如何工作的?因为我Collection在ThreadPoolExecutor或者它的超类中没有看到任何可能的静态变量。
以下作品。结果是“你好世界”
(def ^Callable f (fn [] "hello world"))
(let [e (java.util.concurrent.Executors/newSingleThreadExecutor)]
(try
(.get (.submit e f))
(finally (.shutdown e))))
Run Code Online (Sandbox Code Playgroud)
但以下没有。结果get是nil
(def e (java.util.concurrent.Executors/newSingleThreadExecutor))
(.get (.submit e f))
Run Code Online (Sandbox Code Playgroud)
为什么?我f通过用有副作用的东西替换它来检查它是否被调用。我能看到的唯一区别e是let在一个和def另一个中绑定使用。
另一个问题。如果我没有第一个示例的类型^Callable提示,f则悄悄地返回nil. 它不应该为提交调用抛出异常“找到一个以上的匹配方法”,因为f两者都是Runnable和Callable?如果我像下面这样定义f使用let,则抛出异常
(let [e (java.util.concurrent.Executors/newSingleThreadExecutor)]
(let [f (fn [] "hello world2")]
(try
(.get (.submit e f))
(finally (.shutdown e)))))
Run Code Online (Sandbox Code Playgroud)
谢谢
我打电话时如何确保我的任务能够对中断做出响应Future.cancel()?
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Boolean> future = executor.submit(task);
try {
future.get(timeout, timeoutUnit);
} catch (TimeoutException e) {
future.cancel(true);
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码
public void saveProjects(List<Project> proj) throws DatabaseException {
for (Project listItems: proj) { // error here
insertProjects(listItems);
}
}
private void insertProjects(Project prj) throws DatabaseException {
commitObjects(prj);
}
Run Code Online (Sandbox Code Playgroud)
当我执行上述操作时,我收到以下异常 for (Project listItems: proj) {
java.util.AbstractList中的java.util.ConcurrentModificationException $ Itr.checkForComodification(AbstractList.java:449)at java.util.AbstractList $ Itr.next(AbstractList.java:420)
如何使用next或with iterator解决此问题?
编辑1
我在调用saveProjects的代码片段
projectList.add(proj);
for (Project persist: projectList) {
persist.setProjectId("K7890");
persist.setName(fileName);
myDAO.saveProjects(projectList);
}
projectList.clear();
Run Code Online (Sandbox Code Playgroud) 方法getFirst()和getSecond()这个类的同时被调用.它是Web应用程序的一部分.
内部地图也填充,没有并发.
public class MyClass {
private Map<String, List<List<String>>> first;
private Map<String, List<List<String>>> second;
public MyClass() {
first = new ConcurrentHashMap<>();
second = new ConcurrentHashMap<>();
}
public Set<String> getFirst(String key, String token, int a, int b) {
return get(first, key, token, a, b);
}
public Set<String> getSecond(String key, String token, int a, int b) {
return get(second, key, token, a, b);
}
private Set<String> get(final Map<String, List<List<String>>> map, final String key, final String token,
final int …Run Code Online (Sandbox Code Playgroud) 在设置线程池配置时,如何选择正确的RejectedExecutionHandler?
我有一个发布事件的遗留应用程序(这些事件可以在本地使用,也可以由远程进程使用)。目前,策略是中止,这会导致许多异常和错过的事件。我们将同步队列传递给线程池执行器。
我正在考虑更改RejectedExecutionHandler调用方运行策略。这可能意味着当达到线程限制和队列容量时,调用者会花时间运行该任务。我看不出有什么问题。
到目前为止,您的体验如何?另外,使用无界队列是否意味着没有效用RejectedExecutionHandler?
我试图了解ConcurrentHashMap的工作原理.我找到了一个例子,但我无法理解它.这是它的代码:
Map<String, Object> myData = new HashMap<String, Object>();
myData.put("A", 1);
myData.put("B", 2);
for (String key : myData.keySet()) {
myData.remove(key);
}
Run Code Online (Sandbox Code Playgroud)
这将ConcurrentModificationException在运行时抛出异常.
但是,此代码使用ConcurrentHashMap将正常工作:
Map<String, Object> myData = new ConcurrentHashMap<String, Object>();
myData.put("A", 1);
myData.put("B", 2);
for (String key : myData.keySet()) }
myData.remove(key);
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么ConcurrentHashMap允许在HashMap抛出异常时删除键吗?谢谢