简体   繁体   English

@async方法之间的同步

[英]Synchronization between @async methods

I have to sychronize between asynchronous methods. 我必须在异步方法之间同步。

I have got three methods say 我有三种方法说

method1, method2, method3 with the following defenitions method1,method2,method3具有以下定义

void method1() {
    method2();
    //some code
    method3();
}

@Async
void method2() {
    //some code
}


@Async
void method3() {
    //some code
}

My requirement is that I want to make sure that method3 is executed after method2. 我的要求是我想确保在method2之后执行method3。 After doing some search I have come to a conclusion to use a new SimpleThreadPoolTaskExecutor with pool size configured to 1 and use it for the methods method2 and method3. 经过一些搜索后,我得出一个结论,可以使用池大小配置为1的新SimpleThreadPoolTask​​Executor,并将其用于方法method2和method3。 I would like to know whether there is any better option than this. 我想知道是否有比这更好的选择。 I am new to Spring Task scheduling. 我是Spring Task计划的新手。 And sorry for not providing a demoable example. 非常抱歉,没有提供可演示的示例。

void method1() {
    Semaphore semaphore = new Semaphore(1);
    semaphore.aquire();
    method2(semaphore);
    method3(semaphore);
}


@Async
void method2(Semaphore semaphore) {
   //do some stuff
   semaphore.release();
}

@Async
void method3(Semaphore semaphore) {
    semaphore.aquire();

    // do some stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM