简体   繁体   中英

How can multiple locks speed up multi-threaded code?

How can the synchronized statement speed up the code comparing with synchronized method?

 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.

If stageOne() and stageTwo() were declared synchronized , they would use the same lock. 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.

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.

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. 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.

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 statement locks the block not method so it is faster in comparison to Syncronized method.

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. Syncronized method Demo (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. Syncronized block with one lock (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. Syncronized block with 2 locks (2138 ms)

You can also check the time difference in each case above for executing the same code for different cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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