25.5.3元素
要同时启用@Scheduled和@Async注释,只需在配置中包含任务命名空间中的"annotation-driven"元素.
Run Code Online (Sandbox Code Playgroud)<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/> <task:executor id="myExecutor" pool-size="5"/> <task:scheduler id="myScheduler" pool-size="10"/>请注意,提供了一个执行程序引用,用于处理与具有@Async批注的方法相对应的任务,并提供了调度程序引用以管理使用@Scheduled注释的方法.
反正有没有XML吗?
我有一个单线程执行器服务,用于通过网络获取一些数据.
当用户在搜索框中键入时,我正在排队可能的网络任务.我想要的是取消所有以前的请求,只排队并立即运行最新的请求.
我目前的方法是覆盖execute()和submit()方法并在调用super之前清除队列.
有什么想法吗?
如何实现Callable返回布尔值并做一些事情?
我需要使用外部线程连接到FTP服务器,我不能在主要活动中执行此操作,我需要返回值才能知道它是否已连接;
[主要活动]
public class doSomething implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
// TODO something...
return value;
}
}
public void onClick(View view) {
ExecutorService executor = Executors.newFixedThreadPool(1);
FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new doSomething());
executor.execute(futureTask);
}
Run Code Online (Sandbox Code Playgroud) 我需要一个Java执行程序来拒绝其他正在处理的任务。我想不可能操纵工作队列的大小。
有人可能想知道为什么我首先需要一个具有这种特征的执行者。我需要一种能够轻松更改策略并允许非零队列大小的功能。
有任何想法吗?
如果我在a中运行一个线程,有ExecutorService没有办法知道这个线程在开始执行时没有抛出异常?
我正在做一个像spark这样的spark-submit --class com.mine.myclass --master yarn-cluster --num-executors 3 --executor-memory 4G spark-examples_2.10-1.0.jar
在web ui中,我确实可以看到有3个执行器节点,但每个节点都有2G内存.当我设置--executor-memory 2G时,ui每个节点显示1G.
如何将我的设置减少1/2?
我理解执行者的概念,但我在理解kotlin中的执行者时遇到了一些麻烦.也许它的语法无济于事.
我们来看下面的例子:
private class AlwaysCallback(private val executor: (() -> Unit) -> Unit,
private val cb: Progress.() -> Unit) : Callback {
override fun execute(progress: Progress) {
executor {
progress.cb()
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,遗嘱执行人( () -> Unit ) -> Unit是关闭的船只.要执行的代码块.我不确定这是否属实,或者它只是一个匿名函数的容器.
另一件事是,有人可以解释语法:( () -> Unit ) -> Unit?
我已经阅读了kotlin文档,阅读kotlin源代码并试图谷歌它但我真的很难理解这一点.谢谢
的参数beforeExecute()在ThreadPoolExecutor类是线程和Runnable接口.
有时我们可能需要覆盖此方法以满足我们的需求.
但是,当我在执行程序中提交一些可调用的任务时.在这里,beforeExecute()我们只能得到Runnable对象,所以我想知道执行者是否可以隐式地将可调用的表单转换为runnable?
我在Executors类中找到一些实用方法,转换Runnable为Callable,但我没有找到相反的方法.
我正在使用Executor [固定线程池]和我自己的ThreadFactory添加一个Looper:
Handler HANDLER = new Handler();
Executor THREADS = Executors.newFixedThreadPool(THREAD_POOL_SIZE, new ThreadFactory() {
@Override public Thread newThread(Runnable runnable) {
return new MyThread(new Runnable() {
@Override public void run() {
Looper.prepare();
runnable.run();
}
});
}
});
private static class MyHandler extends Handler {
public boolean fail;
public void handleMessage(Message msg) {
switch(msg.what) {
case 1:
this.fail = msg.arg1 == 1;
Looper.myLooper().quit();
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在运行一个发出网络请求的线程,但如果网络出现故障,我希望向用户显示一条对话框消息.这个过程非常复杂,因为它需要在UI线程中进行AND显示请求.我可以通过简单地向网络线程添加一个循环并等待从UI线程发送消息来等待用户对该对话框的响应.这允许我在一段时间(tryAgain)线程中封装网络请求.一切正常,除非第二次调用Looper.loop()方法(显示第二个网络错误对话框后),并且对话框(在UI线程中)将消息发送到网络线程的处理程序:
THREADS.execute(new Runnable() {
private MyHandler myHandler = new MyHandler();
@Override public void run() …Run Code Online (Sandbox Code Playgroud) 我想知道什么是具有单线程执行程序的RESTful Web服务的最佳体系结构.
我的目标 :
instanciated对象的生命周期非常重要(必须只有一个线程队列).我知道RESTful Web服务生命周期是"每个请求"(类似于我认为的@RequestScoped),所以我看到两个选项:
选项1 :
public class RestService {
protected final static Executor executor;
protected final static Implementation1 impl1;
protected final static Implementation2 impl2;
static {
executor = Executors.newSingleThreadExecutor();
impl1 = new Implementation1();
impl2 = new Implementation2();
}
}
@Path("/servicename")
public class MyService extends RestService {
@POST
@Path("/compute")
public void compute(){
executor.execute(new Runnable(){
public void run(){
impl1.compute();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
选项2:
@Singleton
public class RestService {
private Executor executor; …Run Code Online (Sandbox Code Playgroud) 我正在使用 livy-server-0.2 来运行 spark 作业,但是,我无法更改 spark.executor.cores 的默认设置,它无法生效,而其他设置可以。
它总是使用 1 个核心来启动一个执行程序。
yarn 11893 11889 6 21:08 ? 00:00:01
/opt/jdk1.7.0_80/bin/java -server -XX:OnOutOfMemoryError=kill
%p -Xms1024m -Xmx1024m -Djava.io.tmpdir=/var/lib/hadoop-yarn/cache/yarn/nm-local-dir/usercache/root/appcache/application_1487813931557_0603/container_1487813931557_0603_01_000026/tmp
-Dspark.driver.port=51553
-Dspark.yarn.app.container.log.dir=/var/log/hadoop-yarn/containers/application_1487813931557_0603/container_1487813931557_0603_01_000026
-XX:MaxPermSize=256m org.apache.spark.executor.CoarseGrainedExecutorBackend
--driver-url spark://CoarseGrainedScheduler@10.1.1.81:51553 --executor-id 19
--hostname master01.yscredit.com --cores 1 --app-id application_1487813931557_0603
--user-class-path file:/var/lib/hadoop-yarn/cache/yarn/nm-local-dir/usercache/root/appcache/application_1487813931557_0603/container_1487813931557_0603_01_000026/__app__.jar
Run Code Online (Sandbox Code Playgroud)
这是我在 $SPARK_HOME/conf 中的 spark-defaults.conf 文件
spark.master=yarn
spark.submit.deployMode=cluster
spark.executor.instances=7
spark.executor.cores=6
spark.executor.memoryOverhead=1024
spark.yarn.executor.memoryOverhead=1400
spark.executor.memory=11264
spark.driver.memory=5g
spark.yarn.driver.memoryOverhead=600
spark.speculation=true
spark.yarn.executor.memoryOverhead=1400
Run Code Online (Sandbox Code Playgroud)
有谁能够帮助我?谢谢!
我正在使用Spring(v4)ThreadPoolTaskExecutor执行一些连续的可运行任务。当应用程序关闭时,我希望正常关闭执行器,以便任务有一些时间在继续关闭之前完成其迭代。如果活动任务在执行者等待时间到期之前(例如ThreadPoolTaskExecutor.setAwaitTerminationSeconds())完成了其迭代,则我不希望它开始另一个迭代。到目前为止,我还无法完成此任务。我有以下执行程序配置:
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(30);
Run Code Online (Sandbox Code Playgroud)
我的任务基本上是这样设置的:
myExecutor.execute(() -> {
while(true) {
doSomething();
}
});
Run Code Online (Sandbox Code Playgroud)
我假设我需要在执行程序关闭时在线程中设置一个标志,以使循环中断。还有其他推荐的方法吗?
目前我正在使用Executor:
Executors.newSingleThreadExecutor();
Run Code Online (Sandbox Code Playgroud)
但我的问题是,如果我把它放得太多,那么它们的执行速度太慢而且我的UI会在执行程序完成时挂起.所以我希望它表现得很好,执行者只能一次执行任务.但是当提交新任务时,如果有超过5个任务,我希望他放弃排队任务.
那可能吗?
executor ×13
java ×10
android ×2
apache-spark ×2
spring ×2
annotations ×1
asynchronous ×1
callable ×1
concurrency ×1
core ×1
executors ×1
hadoop-yarn ×1
handler ×1
interrupt ×1
kotlin ×1
lifecycle ×1
livy ×1
looper ×1
rest ×1
web-services ×1