VB_*_*VB_ 3 java spring synchronization
我知道有关重用原型bean的问题已经问过很多次,但是我的问题的意义远不止于此。
问题是什么:
我在for-lookup的处理程序(原型bean)中启动异步任务。但在上一个里程碑达到某个里程碑之前,我无法启动下一个异步任务。因此,仅在上一个任务调用特殊方法之后,才需要进行for循环。
有什么问题:
@Service(value = "Request.Start")
@Scope("prototype")
public static class Start {
public Start() {}
private Object lock;
@Transactional
public void handler(Request request, Response response) {
for (int i = 0; i < request.getAmount(); i++) {
Utils.asyncProcessStart(); //Can't start the next async process before the previous rich some defined milestone
lock.wait();
}
}
public void proceedLookUp() {
lock.notify();
}
}
@Service
public void AsynchronousTask {
public void asyncAction() [
//Needed logic, before start the next async task
getStartHandler().proceedLookUp();
}
public void getStartHandler() {
//HOW TO REWRITE NEEDED PROTOTYPE BEAN
}
}
Run Code Online (Sandbox Code Playgroud)
加成:
问题是什么:我使用Activiti框架,这暗示了一些限制。我应该将一些变量存储到进程(线程)上下文中。我可以将变量写入全局上下文,但不能在进程(线程)启动之前写入本地进程(线程)上下文。
您预期会发生什么,例如request.getAmount()是否返回2?
我应该在两个不同的线程中启动两个异步进程。每个过程具有相同的变量集。我必须将适当的变量写入每个进程(线程)的本地上下文。但是,我CAN NOT做的过程(线程)开始之前(由于Activiti的框架的具体)。
例如,每个进程(线程)应在自己的本地上下文中写入“ id”属性。我在处理程序方法中有列表ID
因此,我应该执行以下一系列操作:
为什么不能只同步调用它?
正如您已经了解的那样,在第二个进程(线程)被for循环覆盖之前,无法保证第一个进程(线程)将“ id”属性写入其本地上下文。
这是我的建议:创建一个单例对象,您可以在线程之间共享该对象以传递信息(状态)。单例使用信号量在线程之间进行协调。您可以使用这种方法将新线程的标识传递回Service类。这是一个简单的示例,显示了我的建议。
测试班:
公共类TestSemaphore {
@Test
public void test() throws Exception {
ThreadCoordinator tc = ThreadCoordinator.getInstance();
for( int i = 0; i < 100; i++ ) {
MyThread r = new MyThread();
r.run();
// This will block until the Thread has called release (after setting its identity on the ThreadCoordinator)
tc.acquire();
String newThreadIdentity = tc.getIdentity();
System.out.println( "Received the new thread's identity: " + newThreadIdentity );
// This will allow the next Thread to acquire the semaphore
tc.release();
}
}
class MyThread extends Thread {
public void run() {
String identity = Integer.toString( (int)(Math.random() * 10000) );
System.out.println( "Running a new thread with identity: " + identity );
// Get a reference to the singleton
ThreadCoordinator tc = ThreadCoordinator.getInstance();
try {
tc.acquire();
tc.setIdentity( identity );
System.out.println( "Notified the ThreadCoordinator from thread: " + identity );
tc.release();
} catch( InterruptedException e ) {
System.out.println( "Caught an interrupted exception: " + e );
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ThreadCoordinator(信号量)类:
import java.util.concurrent.Semaphore;
public class ThreadCoordinator {
private static ThreadCoordinator tc = new ThreadCoordinator();
private static Semaphore semaphore = new Semaphore( 1, true );
private static String identity;
// singleton get instance
public static ThreadCoordinator getInstance() {
return ThreadCoordinator.tc;
}
public void setIdentity( String identity ) throws InterruptedException {
ThreadCoordinator.identity = identity;
}
public String getIdentity() throws InterruptedException {
String identity = ThreadCoordinator.identity;
ThreadCoordinator.identity = null;
return identity;
}
public void acquire() throws InterruptedException {
ThreadCoordinator.semaphore.acquire();
}
public void release() {
ThreadCoordinator.semaphore.release();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1753 次 |
| 最近记录: |