简体   繁体   English

多个锁如何加速多线程代码?

[英]How can multiple locks speed up multi-threaded code?

How can the synchronized statement speed up the code comparing with synchronized method? 该如何synchronized语句加快代码同步的方法相比?

 public void stageOne() {

    synchronized (lock1) {
        list1.add(random.nextInt(100));
    }
}

public void stageTwo() {

     synchronized (lock2) {
        list2.add(random.nextInt(100));
    }
}
public void process() {
    for (int i = 0; i < 1000; i++) {
        stageOne();
        stageTwo();
    }
}


    Thread t1 = new Thread(new Runnable() {
        public void run() {
            process();
        }
    });

    Thread t2 = new Thread(new Runnable() {
        public void run() {
            process();
        }
    });

If I use public synchronized void stageOne() and public synchronized void stageTwo() , the program will take more time to finish. 如果我使用public synchronized void stageOne()public synchronized void stageTwo() ,程序将花费更多时间来完成。

If stageOne() and stageTwo() were declared synchronized , they would use the same lock. 如果stageOne()stageTwo()被声明为synchronized ,则它们将使用相同的锁。 That lock would be the object that contains these methods. 该锁定将是包含这些方法的对象。 This would mean that the methods cannot be executed simultaneously. 这意味着这些方法不能同时执行。

Since you have two methods and two threads, you will have four methods competing for the same lock. 由于您有两个方法和两个线程,因此您将有四种方法竞争同一个锁。 If both methods have their own locks, as in the code you supplied, one instance of each method can be executed at the same time. 如果两个方法都有自己的锁,就像在您提供的代码中一样, 每个方法的一个实例可以同时执行。

Let's say each stage needs 1 second to execute, and you start 2 threads. 假设每个阶段需要1秒才能执行,并且您启动2个线程。

With a global lock, if one thread executes any of the method, the other thread can't execute any of the method. 使用全局锁定,如果一个线程执行任何方法,则另一个线程无法执行任何方法。 So the total time will be 2 seconds + 2 seconds = 4 seconds. 所以总时间为2秒+2秒= 4秒。

T1
S1 ---- S2 --- end

T2
wait----------- S1 ---- S2 ---- end

With 2 locks, once the first thread has finished executing stage1, the second one can execute stage1 while the first one executes stage2. 使用2个锁,一旦第一个线程完成执行stage1,第二个线程可以执行stage1,而第一个执行stage2。 So, assuming the tasks are completely parallelizable and you have 2 cores, the second thread will wait for 1 second, then do its two stages, and the total time will thus be 3 seconds. 因此,假设任务完全可并行化并且您有2个内核,则第二个线程将等待1秒,然后执行其两个阶段,因此总时间将为3秒。

T1
S1 ---- S2 --- end

T2
wait--- S1 ----S2 ---- end

Syncronized method locks the whole method.So only one thread can execute that method causing other thread to wait. Syncronized方法锁定整个方法。因此只有一个线程可以执行该方法,导致其他线程等待。

Syncronized statement locks the block not method so it is faster in comparison to Syncronized method. Syncronized语句锁定块而不是方法,因此与Syncronized方法相比,它更快。

Example 1(Syncronized method) :Suppose there is a public toilet .There are 2 person and they both have to pee (as fast as they can) but only one can enter the toilet and when it enters it locks the door .In this case Person 2 has to waits outside the door for Person 1 to free the toilet.So same in case of Syncronized method thread 2 waits outside the method for thread 1 to free the lock. 实施例1(Syncronized法):假设有a public toilet 。有2 person ,他们都必须pee (如快,因为他们可以),但只有一个可以进入厕所和当它进入它locks门。在这种情况下Person 2必须waits门外waits Person 1以释放厕所。因此,在Syncronized method情况下,线程2在线程1的方法之外等待Syncronized method锁定。 Syncronized method Demo (8028 ms) 同步方法演示 (8028 ms)

Example 2(Syncronized block) :Suppose there is a public toilet .There are 2 person and they both have to pee (as fast as they can) but in this case they both can enter in the toilet but when they enters they find that there is only one urinal in the toilet.But in this case Person 2 is waiting inside the toilet door for Person 1 to free the urinal .That doesn't make much difference waiting outside the door or inside but there is a difference(maybe for few seconds) .So same in case of Syncronized block thread 2 wait inside the function but outside the block for thread 1 to free the lock.So the syncronized block takes less time than syncronized method. 示例2(同步块) :假设有a public toilet 。有2 person ,他们都必须pee (尽可能快)但在这种情况下,他们都可以enter厕所,但当他们进入他们发现那里厕所里只有one urinal 。但是在这种情况下, Person 2正在厕所门内等Person 1以释放小便池。在门外或室内等待并没有太大区别但是有一点difference(maybe for few seconds) 。所以在的情况下,相同Syncronized block线程2等待功能内,但对于线程1的块之外以释放lock.So的syncronized块花费的时间比syncronized方法更少的时间。 Syncronized block with one lock (4252 ms) 带一锁的同步块 (4252 ms)

The real advantage is in using 2 or more locks for different threads like when in second example when 2 person enters toilet they find 2 urinals they both can use different urinals at the same time .Same thread 1 holds lock 1 for functionn 1 and at the same time thread 2 holds lock 2 for function 2 and later exchnage when they are free .That would probably save more time. real advantage是对于不同的线程使用2个或more locks ,就像在第二个例子中当2个人进入厕所时他们找到2 urinals他们都可以同时使用不同的小便器。同一个thread 1 holds lock 1 for functionn 1 and at the same time thread 2 holds lock 2用于功能2,以及稍后的功能,当它们是空闲时。这可能会节省更多时间。 Syncronized block with 2 locks (2138 ms) 带2个锁的同步块 (2138 ms)

You can also check the time difference in each case above for executing the same code for different cases. 您还可以检查上述每种情况下的时差,以便针对不同情况执行相同的代码。

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

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