Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Internal lock: synchronized keyword

Each object in Java has an Intrinsic lock associated with it. Also known as a Monitor, this internal lock is an exclusive lock that guarantees atomicity, visibility, and order.

Internal locking is implemented with the synchronized keyword, which modifies the code block and modifies the method.

Modify the syntax of a code block:

Synchronized {

Synchronized code blocks in which shared data can be accessed

}

Modifying instance methods is called synchronous instance methods

Modifying static methods is called synchronous static methods

In the last article, we talked about synchronized code blocks. Today, we’ll talk about thread synchronization methods

2. Synchronization method

package com.wkcto.intrinsiclock; /** * Synchronized instance methods * use the entire method body as a synchronized code block * The default lock object is this object * Author: Public class Test05 {public static void main(String[] args) {public static void main(String[] args) Test05(); Thread(new Runnable() {@override public void run() {obj.mm(); }}).start(); New Thread(new Runnable() {@override public void run() {obj. // new Test05().mm22(); }}).start();}}).start(); Public void mm(){synchronized (this){for (int I = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + " --> " + i); Public synchronized void mm22(){for (int I = 1; public synchronized void mm22(){for (int I = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + " --> " + i); }}}Copy the code
package com.wkcto.intrinsiclock; * The default lock object is the runtime class object of the current class, test06.class, which some call class lock * Author: Public class Test06 {public static void main(String[] args) {public static void main(String[] args) Test06(); New Thread(new Runnable() {@override public void run() {obj.m1(); // Use the lock object test06.class}}).start(); New Thread(new Runnable() {@override public void run() {test6.sm2 (); // Use the lock object test06.class}}).start(); Synchronized (test06.class) public void m1(){synchronized (test06.class) synchronized (test06.class) synchronized (test06.class) { for (int i = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + " --> " + i); Public synchronized static void sm2(){for (int I = 1; public synchronized static void sm2(); i <= 100; i++) { System.out.println(Thread.currentThread().getName() + " --> " + i); }}}Copy the code
package com.wkcto.intrinsiclock; /** * How to select synchronous method and synchronous code block Public class Test07 {public static void main(String[] args) {Test07 obj = new Test07(); new Thread(new Runnable() { @Override public void run() { obj.doLongTimeTask(); } }).start(); new Thread(new Runnable() { @Override public void run() { obj.doLongTimeTask(); } }).start(); } public synchronized void doLongTimeTask(){try {system.out.println ("Task Begin"); Thread.sleep(3000); System.out.println(" Start sync "); for(int i = 1; i <= 100; i++){ System.out.println(Thread.currentThread().getName() + "-->" + i); } System.out.println("Task end"); } catch (InterruptedException e) { e.printStackTrace(); Public void doLongTimeTask2(){try {system.out.println ("Task Begin"); Thread.sleep(3000); Synchronized (this){system.out.println (" start synchronization "); synchronized (this){system.out.println (" start synchronization "); for(int i = 1; i <= 100; i++){ System.out.println(Thread.currentThread().getName() + "-->" + i); } } System.out.println("Task end"); } catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code