Java thread synchronization methods, method block differences

Source: Internet
Author: User

First, the synchronization method, whether it is the current object locked or the current class

Code Block 1

Package Com.ssss;public class Thread1 implements Runnable {  //public static object  O=new object ();    public void Run () {    pt ();    }        Public synchronized Void Pt () {    int a=0;        Synchronized (o) {for               (int i = 0; i < 5; i++) {             a++;             try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}                  System.out.println (Thread.CurrentThread (). GetName () + "synchronized loop" + i + "++++" + a);               }          }      } public            static void Main (string[] args) {           Thread1 t1 = new Thread1 ();           Thread1 t2 = new Thread1 ();           Thread ta = new Thread (T1, "A");           Thread TB = new Thread (t2, "B");           Ta.start ();           Tb.start ();      } }

The printed result is


This shows that the current object, and A and B are two different objects, so the print sequence is confusing


Code Block 2

Look at the sync block

Package Com.ssss;public class Thread1 implements Runnable {public  static object  O=new object ();    public void Run () {    pt ();    }        public void Pt () {    int a=0;        Synchronized (o) {for               (int i = 0; i < 5; i++) {             a++;             try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}                  System.out.println (Thread.CurrentThread (). GetName () + "synchronized loop" + i + "++++" + a);            }} public static void Main (string[] args) {           Thread1 t1 = new Thread1 ();           Thread1 t2 = new Thread1 ();           Thread ta = new Thread (T1, "A");           Thread TB = new Thread (t2, "B");           Ta.start ();           Tb.start ();      } }

Look at the print sequence


Description Sync block can lock any object


That is, the synchronization method locks the current object, and the synchronous method block can lock any object



Java thread synchronization methods, method block differences

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.