Multithreading synchronized Keywords

Source: Internet
Author: User

In multithreaded situations, when multiple threads operate on the same resource, a security issue occurs, such as dirty reads (when a thread reads a variable, the value has been changed by another thread).

Synchronized keyword: can be used to synchronize methods or blocks of code. About synchronized, summarize a few.

The 1 synchronized keyword lock is an object that, when multiple objects create multiple locks, does not reach the effect of synchronization.

2 locks are required only when operating public resources, and non-public resources are not required to be locked.

The 3 synchronized keyword has a reentrant lock.

4 When an exception occurs, the lock is automatically released.

5 synchronization does not have inheritance.

The 6 sleep () method does not release the lock.

The 7wait () method releases the lock.

8 synchronized can be synchronized, or you can synchronize code blocks.

Several of them are verified below;

Method class:

public class MyMethod {synchronized public void MethodA (String username) throws Interruptedexception{system.out.println (username), if (Username.equals ("a")) {System.out.println (Thread.CurrentThread (). GetName () + "into  MethodA"); Thread.Sleep (2000); System.out.println (Thread.CurrentThread (). GetName () + "out  MethodA");} else {System.out.println (Thread.CurrentThread (). GetName () + "into  MethodB"); Thread.Sleep (1000); System.out.println (Thread.CurrentThread (). GetName () + "out  MethodB");} /*while (True) {}*/}//synchronized Lock code block public static void MethodB (String lock) throws interruptedexception{synchronized (lock) {System.out.println (Thread.CurrentThread (). GetName () + "into  lock"); Thread.Sleep (1000); System.out.println (Thread.CurrentThread (). GetName () + "Out  lock");}}

  

Main thread: Synchronized synchronous code block. The synchronized (this) lock is the current object.

public class Run {public static void main (string[] args) throws interruptedexception {MyMethod m1=new MyMethod (); MyMethod m2=new MyMethod (); String lock= ""; Thread T1 =new Thread (new Runnable () {@Overridepublic void run () {try {M1.methodb (lock);} catch (Interruptedexception e) { TODO auto-generated catch Blocke.printstacktrace ();}}, "T1"); Thread T2 =new Thread (new Runnable () {@Overridepublic void run () {try {M1.methodb (lock);} catch (Interruptedexception e) { TODO auto-generated catch Blocke.printstacktrace ();}}, "T2"); T1.start (); T2.start ();}}

Console:

T2 to  lockt2 out lockt1 to lockt1 out  lock

Synchronized synchronization method, modify the main thread as follows

public class Run {public static void main (string[] args) throws interruptedexception {MyMethod m1=new MyMethod (); MyMethod m2=new MyMethod (); String lock= ""; Thread T1 =new Thread (new Runnable () {@Overridepublic void run () {try {M1.methoda ("a");} catch (Interruptedexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();}}}, "T1"); Thread T2 =new Thread (new Runnable () {@Overridepublic void run () {try {M1.methoda ("B");} catch (Interruptedexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();}}, "T2"); T1.start (); T2.start ();}}

Console output:

BT2 to  methodBt2 out METHODBAT1 to methodAt1 out  MethodA

You can find the effect of synchronizing.

Re-modify the main thread code as follows:

public class Run {public static void main (string[] args) throws interruptedexception {MyMethod m1=new MyMethod (); MyMethod m2=new MyMethod (); String lock= ""; Thread T1 =new Thread (new Runnable () {@Overridepublic void run () {try {M1.methoda ("a");} catch (Interruptedexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();}}}, "T1"); Thread T2 =new Thread (new Runnable () {@Overridepublic void run () {try {M2.methoda ("B");} catch (Interruptedexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();}}, "T2"); T1.start (); T2.start ();}}

The console is as follows

BAT1 into  methodAt2 to methodBt2 out methodBt1 out  MethodA

and executes the main method multiple times, discovering that the console prints in a different order. The call here is two objects, so the JVM creates two locks, which do not affect each other, so in a lock, you can only lock methods in an object. Proof that the synchronized lock is an object. In this process, I also tested the static method when the MethodA () method was changed to static

Two objects will have the same effect as the synchronized one.

Reentrant Lock Concept: You can get your own internal lock again. If a thread acquires a lock on an object, the object is not released at this time, and when it wants to acquire the lock of the object again, it can get it, otherwise it will cause a deadlock.

  

  Every good person has a silent time. No complaining, no complaints, finally through the passage of the time to move their own days.

Multithreading synchronized Keywords

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.