Home  >  Article  >  Backend Development  >  Detailed explanation of using synchronized to implement a Lock code

Detailed explanation of using synchronized to implement a Lock code

php中世界最好的语言
php中世界最好的语言Original
2017-12-20 11:56:032442browse

This article introduces how to use synchronized to implement a Lock code. The following is a practical case. Friends in need can refer to it.

Method one:

public synchronized void a(){
  //TODO
}


Method two:

public void b(){
  synchronized(this){
    //TODO
  }
}


From these two ways Look, the locks are all added between {}. Let’s take a look at how Lock is done:

public void c() {
  lock.lock();
  try {
    // TODO
  } finally {
    lock.unlock();
  }
}


The lock in this way is added between lock( ) and unlock(), so if you want to implement a lock function, you must think about how to implement these two methods, the lock() and unlock() methods. First define a framework as follows:

public void lock(){
}
public void unlock(){
}


Then think about how to use synchronized to implement these two methods.

Now I actually have a slightly clearer idea, but I still don’t know how to fill in these two methods. I will analyze the characteristics of Lock later, and then look at this code:

public void c() {
    lock.lock();
    //When current thread get the lock, other thread has to wait
    try {
        //current thread get in the lock, other thread can not get in
        // TODO
    }
    finally {
        lock.unlock();
        //current thread release the lock
    }
}


I just added a few comments to this code and did nothing else. Does it help to understand this code and see if it appears most frequently? What is the word? It is currentthread. So when we fill in the lock() and unlock() methods, should we pay attention to the keyword currentthread to find the solution? The answer is yes.

Next, analyze, how to make the thread wait when using synchronized? Use the wait() method. How to wake up the thread? Use the notify() method. Then use the wait() method in the lock() method and the notify() method in the unlock() method. So there is a condition when we use wait() and notify(). Think about what we should use as the condition?

We should use whether the current lock is occupied as a judgment condition. If the lock is occupied, currentthread waits. Think about whether we have always used this condition when using synchronized, the answer is yes.

Let’s analyze when to release the lock and what conditions are used. Think about it. If thread A gets the lock, can thread B release it? Of course not. If B could be released, it would violate the principle. Of course not. It must be that thread A's lock can only be released by thread A. So the judgment condition is to judge whether the thread holding the lock is currentthread. If so, it can be released. If not, of course it cannot.

Now let’s take a look at the complete code:

package test.lock;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class NaiveLock {
    private static final long NONE = -1;
    private long owner = NONE;
    private Boolean isLooked() {
        return owner != NONE;
    }
    public synchronized void lock() {
        long currentThreadId = Thread.currentThread().getId();
        if (owner == currentThreadId) {
            throw new IllegalStateException("Lock has been acquired by current thread");
        }
        while (this.isLooked()) {
            System.out.println(String.format("thread %s is waitting lock", currentThreadId));
            try {
                wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        owner = currentThreadId;
        System.out.println(String.format("Lock is acquired by thread %s", owner));
    }
    public synchronized void unlock() {
        if (!this.isLooked() || owner != Thread.currentThread().getId()) {
            throw new IllegalStateException("Only Lock owner can unlock the lock");
        }
        System.out.println(String.format("thread %s is unlocking", owner));
        System.out.println();
        owner = NONE;
        notify();
    }
    public static void main(String[] args) {
        final NaiveLock lock = new NaiveLock();
        ExecutorService executor = Executors.newFixedThreadPool(20, new ThreadFactory() {
            private ThreadGroup group = new ThreadGroup("test thread group");
            {
                group.setDaemon(true);
            }
            @Override
                  public Thread newThread(Runnable r) {
                return new Thread(group, r);
            }
        }
        );
        for (int i = 0; i < 20; i++) {
            executor.submit(new Runnable() {
                @Override
                        public void run() {
                    lock.lock();
                    System.out.println(String.format("thread %s is running...", Thread.currentThread().getId()));
                    try {
                        Thread.sleep(new Random().nextint(1000));
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            }
            );
        }
    }
}


##Run it and see the result:

Lock is acquired by thread 8
thread 8 is running...
thread 27 is waitting lock
thread 26 is waitting lock
thread 25 is waitting lock
thread 24 is waitting lock
thread 23 is waitting lock
thread 22 is waitting lock
thread 21 is waitting lock
thread 20 is waitting lock
thread 19 is waitting lock
thread 18 is waitting lock
thread 17 is waitting lock
thread 16 is waitting lock
thread 15 is waitting lock
thread 14 is waitting lock
thread 13 is waitting lock
thread 12 is waitting lock
thread 11 is waitting lock
thread 10 is waitting lock
thread 9 is waitting lock
thread 8 is unlocking
  
Lock is acquired by thread 27
thread 27 is running...
thread 27 is unlocking
  
Lock is acquired by thread 26
thread 26 is running...
thread 26 is unlocking
  
Lock is acquired by thread 25
thread 25 is running...
thread 25 is unlocking
  
Lock is acquired by thread 24
thread 24 is running...
thread 24 is unlocking
  
Lock is acquired by thread 23
thread 23 is running...
thread 23 is unlocking
  
Lock is acquired by thread 22
thread 22 is running...
thread 22 is unlocking
  
Lock is acquired by thread 21
thread 21 is running...
thread 21 is unlocking
  
Lock is acquired by thread 20
thread 20 is running...
thread 20 is unlocking
  
Lock is acquired by thread 19
thread 19 is running...
thread 19 is unlocking
  
Lock is acquired by thread 18
thread 18 is running...
thread 18 is unlocking
  
Lock is acquired by thread 17
thread 17 is running...
thread 17 is unlocking
  
Lock is acquired by thread 16
thread 16 is running...
thread 16 is unlocking
  
Lock is acquired by thread 15
thread 15 is running...
thread 15 is unlocking
  
Lock is acquired by thread 14
thread 14 is running...
thread 14 is unlocking
  
Lock is acquired by thread 13
thread 13 is running...
thread 13 is unlocking
  
Lock is acquired by thread 12
thread 12 is running...
thread 12 is unlocking
  
Lock is acquired by thread 11
thread 11 is running...
thread 11 is unlocking
  
Lock is acquired by thread 10
thread 10 is running...
thread 10 is unlocking
  
Lock is acquired by thread 9
thread 9 is running...
thread 9 is unlocking


If you change the

for loop to 30 times, look at the result again:

Lock is acquired by thread 8
thread 8 is running...
thread 27 is waitting lock
thread 26 is waitting lock
thread 25 is waitting lock
thread 24 is waitting lock
thread 23 is waitting lock
thread 22 is waitting lock
thread 21 is waitting lock
thread 20 is waitting lock
thread 19 is waitting lock
thread 18 is waitting lock
thread 17 is waitting lock
thread 16 is waitting lock
thread 15 is waitting lock
thread 14 is waitting lock
thread 13 is waitting lock
thread 12 is waitting lock
thread 11 is waitting lock
thread 10 is waitting lock
thread 9 is waitting lock
thread 8 is unlocking
  
Lock is acquired by thread 27
thread 27 is running...
thread 8 is waitting lock
thread 27 is unlocking
  
Lock is acquired by thread 27
thread 27 is running...
thread 26 is waitting lock
thread 27 is unlocking
  
Lock is acquired by thread 27
thread 27 is running...
thread 25 is waitting lock
thread 27 is unlocking
  
Lock is acquired by thread 24
thread 24 is running...
thread 27 is waitting lock
thread 24 is unlocking
  
Lock is acquired by thread 23
thread 23 is running...
thread 24 is waitting lock
thread 23 is unlocking
  
Lock is acquired by thread 22
thread 22 is running...
thread 23 is waitting lock
thread 22 is unlocking
  
Lock is acquired by thread 22
thread 22 is running...
thread 21 is waitting lock
thread 22 is unlocking
  
Lock is acquired by thread 22
thread 22 is running...
thread 20 is waitting lock
thread 22 is unlocking
  
Lock is acquired by thread 22
thread 22 is running...
thread 19 is waitting lock
thread 22 is unlocking
  
Lock is acquired by thread 22
thread 22 is running...
thread 18 is waitting lock
thread 22 is unlocking
  
Lock is acquired by thread 17
thread 17 is running...
thread 17 is unlocking
  
Lock is acquired by thread 16
thread 16 is running...
thread 16 is unlocking
  
Lock is acquired by thread 15
thread 15 is running...
thread 15 is unlocking
  
Lock is acquired by thread 14
thread 14 is running...
thread 14 is unlocking
  
Lock is acquired by thread 13
thread 13 is running...
thread 13 is unlocking
  
Lock is acquired by thread 12
thread 12 is running...
thread 12 is unlocking
  
Lock is acquired by thread 11
thread 11 is running...
thread 11 is unlocking
  
Lock is acquired by thread 10
thread 10 is running...
thread 10 is unlocking
  
Lock is acquired by thread 9
thread 9 is running...
thread 9 is unlocking
  
Lock is acquired by thread 8
thread 8 is running...
thread 8 is unlocking
  
Lock is acquired by thread 26
thread 26 is running...
thread 26 is unlocking
  
Lock is acquired by thread 25
thread 25 is running...
thread 25 is unlocking
  
Lock is acquired by thread 27
thread 27 is running...
thread 27 is unlocking
  
Lock is acquired by thread 24
thread 24 is running...
thread 24 is unlocking
  
Lock is acquired by thread 23
thread 23 is running...
thread 23 is unlocking
  
Lock is acquired by thread 21
thread 21 is running...
thread 21 is unlocking
  
Lock is acquired by thread 20
thread 20 is running...
thread 20 is unlocking
  
Lock is acquired by thread 19
thread 19 is running...
thread 19 is unlocking
  
Lock is acquired by thread 18
thread 18 is running...
thread 18 is unlocking


I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

Detailed code examples of how to implement stack data structure and bracket matching algorithm in php

The most popular in php Simple string matching algorithm, php matching algorithm_PHP tutorial

The simplest string matching algorithm tutorial in php

The above is the detailed content of Detailed explanation of using synchronized to implement a Lock code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn