Java thread synchronization method, method block difference, java thread synchronization difference

Source: Internet
Author: User

Java thread synchronization method, method block difference, java thread synchronization difference

First, let's talk about the synchronization method. Is it the locked current object 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(1000);} 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


It can be seen that it is the current object, while A and B are two different objects, so the print sequence is messy.


Code Block 2

View synchronization Blocks

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(1000);} 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();      } }

View print Sequence


It indicates that the synchronization block can lock any object.


That is to say, the synchronization method is to lock the current object, and the synchronization method block can lock any object.



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.