[英]More threads to synchronized method of same instance
What happens when 1000 requests(each Httprequest as one thread) came to server that calls deposit method of Trust object. 当1000个请求(每个Httprequest作为一个线程)到达服务器并调用Trust对象的deposit方法时,会发生什么情况。
I have written code like this which works fine, but what is the case if deposit amount had long logic that makes other threads in waiting state. 我已经写了这样的代码,可以很好地工作,但是如果存入金额具有较长的逻辑,这会使其他线程处于等待状态,该怎么办?
class Trust {
private int amount;
public synchronized void deposit(int amount) {
this.amount += amount;
System.out.println(Thread.currentThread().getName() + "; Amount " +this.amount);
}
}
class DepositAmount implements Runnable {
Trust obj;
private int amount;
DepositAmount(Trust obj, int amount) {
this.obj = obj;
this.amount += amount;
}
public void run() {
obj.deposit(amount);
}
}
public class Bank {//CustomerToTrust {
public static void main(String args[]) {
System.out.println("Naga");
//amt.nextInt(5000)
Trust obj = new Trust();
for(int i =0; i< 100 ; i++) {
Random amt = new Random();
new Thread(new DepositAmount(obj, 100)).start();
}
}
}
In a case deposit amount method has long logic, Please tell me suppose when 1000 requests came at one instance to deposit amount into trust. 如果存入金额的方法逻辑很长,请告诉我,假设一次有1000个请求将存入金额信任。 Will the remaining 999 threads will get blocked till the first thread deopists amount.
剩下的999个线程是否会被阻塞,直到第一个线程不再使用该线程。 What about the last thread, the user needs to wait till that time for the response.
关于最后一个线程,用户需要等到那个时间才能得到响应。
"Will the remaining 999 threads will get blocked till the first thread deopists amount" “剩余的999个线程将被阻塞,直到第一个线程的操作员数量达到为止”
Yes. 是。 The first thread will enter the deposit method.
第一个线程将进入存款方法。 Any other threads that want to do the same will enter a blocked state until the resource becomes free.
任何其他想要执行此操作的线程都将进入阻塞状态,直到资源可用。
Yes, all other threads using the synchronised "deposit" method on Trust have to wait for the first caller to finish it's method call on deposit. 是的,在Trust上使用同步的“ deposit”方法的所有其他线程必须等待第一个调用方完成其对存款的方法调用。 That's a little tricky with your shared state "Trust"...
与您的共享状态“信任”有点棘手...
You should have a separated thread... This thread a.) gives other threads the opportunity to add "request for work" (a synchronised method, which just puts the requests in a queue) and a method within this thread working on that queue... With that you would have decoupled the processes of need to wait for the whole processing to be finished (because the threads only need to wait until the request has been put to the queue). 您应该有一个单独的线程...该线程a。)使其他线程有机会添加“工作请求”(同步方法,该方法将请求放入队列中),并且该线程中的方法可以对该队列进行工作...这样一来,您便可以将需要等待整个处理完成的过程分离开来(因为线程只需要等待,直到将请求放入队列中即可)。
The threads will call deposit()
one at a time. 线程将一次调用一次
deposit()
。 Thus the 1000th request will block until the other 999 have finished. 因此,第1000个请求将被阻塞,直到另一个999完成为止。
There are two ways to improve performance: 有两种提高性能的方法:
synchronized
block as quick as possible. synchronized
块内执行的操作尽可能快。 The latter could be implemented using AtomicInteger
: 后者可以使用
AtomicInteger
实现:
class Trust {
private AtomicInteger balance = new AtomicInteger(0);
public void deposit(int amount) {
int newBalance = this.balance.addAndGet(amount);
System.out.println(Thread.currentThread().getName() +
"; Balance " + newBalance);
}
}
Note that, even though this implementation is correct in terms of updating the account, the output strings could be printed out of order. 请注意,即使此实现在更新帐户方面是正确的,也可能会无序打印输出字符串。
Also see Amdahl's Law . 另请参阅阿姆达尔定律 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.