This is the 18th day of my participation in the August Challenge

Multithreading has been the difficulty in Java development, but also in the interview of frequent, while there is still time, I intend to consolidate the JUC knowledge, I think the opportunity is everywhere, but is always left to prepare for the people, I hope we can come on!!

Go down, come up again, I think we’ll be different.

Before we talk about the Lock interface, let’s review the Synchronized keyword. I don’t think we’ve been learning Java development for so long that we still have a friend who hasn’t used Synchronized

The cover was contributed by a large circle of friends

JUC series

  • What is JUC?
  • JUC series (ii) Review Synchronized keyword
  • JUC series (three) Lock Lock mechanism detail code theory combined
  • The thread safety problem of JUC series (4)
  • JUC series (5) | Synchonized keywords to further explain
  • JUC series (6) | Callable and interface explanation & use, FutureTask application Future
  • JUC series (7) | JUC three commonly used tools CountDownLatch, CyclicBarrier, Semaphore
  • JUC series (8) | read-write lock – ReadWriteLock

Is being continuously updated…

I. Scope of synchronized:

Synchronized is a Java keyword and a type of synchronization lock. It modifies the following objects:

  1. Modifies a code block. The modified code block is called a synchronous block. Scope is between {}. The object used is the object that called the code block.

    synchronized (this){
    	System.out.println("Synchronized code block");
    }
    Copy the code
  2. In terms of methods, the method being modified is called a synchronous method. The scope is the entire method. The object used is the object that called the method.

    public synchronized void sale(a) {}Copy the code

    Synchronized cannot be inherited. If a method in a parent class uses the synchronized keyword and the synchronized class overwrites it, the synchronized class is not synchronized by default and must be explicitly added to the method in the subclass. Of course, if you call a synchronized method in a word class, then even though the word class doesn’t have a synchronized method, but the subclass calls a synchronized method in the parent class, the subclass’s methods are pretty synchronized.

  3. Pertaining to a static method that applies to the entire static method and to all objects of the class.

        public static synchronized void test(a){}Copy the code
  4. Pertaining to a class that applies to all objects of the class enclosed in parentheses after synchronized.

    class Ticket {
        public void sale(a) {
            synchronized (Ticket.class) {
    
            }
        }
    }
    Copy the code

Ii. Cases:

Synchronized (synchronized)

public class SynchronizedDemo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        
        new Thread(() -> {
            for (int i = 0; i < 40; i++) { ticket.sale(); }},"A").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) { ticket.sale(); }},"B").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) { ticket.sale(); }},"C").start(); }}class Ticket {
    / / votes
    private int number = 30;

    // How to sell tickets
    public synchronized void sale(a) {
        // Check whether there are tickets
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + ":" + (number--) + ""+ number); }}}Copy the code

Iii. Summary:

Synchronized is very inefficient because if a block of code is modified by synchronized, when a thread enters a block of synchronized code, the other threads must wait until the thread that acquired the lock releases the lock before entering the block again.

The thread that acquired the lock can release the lock in only two ways:

  1. Execute normally, then release the lock.
  2. During execution, an exception occurs and the JVM tells the thread to release the lock automatically.

If a thread that has acquired the lock is blocked due to waiting IO or other reasons, but cannot release the lock, other threads can only watch at the door of synchronized. Think about this efficiency. 😂

Therefore, it is very necessary to have a mechanism to prevent waiting threads from waiting indefinitely, such as waiting for a period of time or responding to interrupts. How to do this?

We can do this with Lock.

4. Talk to yourself

Learn more about Lock in the next article.

Recently, I started to learn JUC again. I feel there is a lot of Java content, but I still think I need to lay a solid foundation in order to go further.

Recently in the continuous update, if you feel helpful to you, also interested in the words, pay attention to me, let us study together, discuss together.

Hello, I am ning Zhichun, a blogger, a small seed on the way of Learning Java. I also hope that one day I can take root and grow into a tree in the sky.

Hope to share with you 😁

By the time we meet again, we have achieved something.