简体   繁体   English

更多线程可以同步同一实例的方法

[英]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: 有两种提高性能的方法:

  1. Make the operations that are performed inside the synchronized block as quick as possible. 使在synchronized块内执行的操作尽可能快。
  2. Use a non-blocking algorithm . 使用非阻塞算法

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.

相关问题 我们可以同时从多个线程访问同一个实例的同步方法和非同步方法吗? - Can we Access Synchronized method and an unsynchronized method of same instance from multiple threads at the same time? 在同一实例上使用同步方法锁定 - Locking with synchronized method on same instance 多个线程执行相同的方法(非同步)行为 - Multiple Threads executing same Method (Non Synchronized) behavior 当少数线程尝试调用相同的同步方法时会发生什么? - What happens when few threads trying to call the same synchronized method? 两个线程可以同时访问同步方法吗? - Can two threads access a synchronized method at the same time? 使用同一实例从另一个线程访问同步方法 - Access synchronized method from another thread using same instance 实例的同步方法可以被同步语句锁定吗 - Can a synchronized method of an instance be locked by synchronized statement Java:多个线程通过同步方法同时访问变量来减少静态变量 - Java: Multiple threads decrementing static variable via synchronized method accessing variable at the same time 需要代码来理解同步的 static 和同一实例上的两个不同线程同时访问的非静态方法 - Need code to understand synchronized static and non-static methods being accessed at the same time by two different threads on the same instance 多线程使用的Java同步和静态同步方法 - Java Synchronized and static synchronized method used by Multiple Threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM