0

My question is about threads being queued. For my example I have one Spring context. I have a method named CalculateTax in a stateless class. A request comes in, a thread is created (tA) and it eventually enters the CalculateTax method. Within the same "time frame" another request comes in and another thread is created (tB). Now, here is what I want to understand. AFAIK tB cannot execute CalculateTax until tA has exited the method. Is this true?

4

3 回答 3

3

只要CalculateTax 只使用局部变量(即在方法中声明),您就不会有任何线程同步问题,并且多个线程可以毫无问题地调用该方法。

但是,如果由于某种原因 CalculateTax 使用在类级别定义的变量,并且您使用的是单例模式(您将问题标记为“单例”,所以我猜您是这样),您可能会遇到线程同步问题。

于 2009-10-26T10:34:41.600 回答
2

不,如果它们是并行线程,则不是这样,每个线程都在自己的执行堆栈中,因此它应该能够在 tA 执行时执行。

这就是线程的用途。

于 2009-10-26T10:28:25.967 回答
0

一般来说,答案是不确定的。如果您的“请求”来自远程客户端,则答案取决于用于服务公开的机制的实现细节。

但是,我不知道真正使代理序列化请求的远程通信框架,即假设由目标服务开发人员解决(例如,您的任务是为服务实现提供线程安全或使用序列化所有请求显式同步等)。

于 2009-10-26T10:39:53.850 回答