Static synchronization of threads and non-static differences

Source: Internet
Author: User

Static synchronization of threads and non-static differences
So what is the difference between adding synchronized to the static and non-static methods?


As we all know, the static method belongs to the Class method, and it belongs to this Class (Note: The Class here does not refer to a specific pair of Class
Such as), the lock obtained by static is the Class of the object currently calling this method (Class, instead of produced by this Class ).
). Instead of the lock obtained by the static method, it is the lock of the object currently calling this method. So no between them
Will generate mutex.

package com.jack.zhang.chapter9.classlock;/** *  * @author EX-LIUQI001 *  */public class Test {public static synchronized void staticX() throws InterruptedException {for (int i = 0; i < 10; i++) {Thread.sleep(1000);System.out.println("staticX.......................");}}public synchronized void x() throws InterruptedException {for (int i = 0; i < 10; i++) {Thread.sleep(1000);System.out.println("x.......................");}}public static void main(String[] args) {final Test test1 = new Test();Thread thread = new Thread(new Runnable() {public void run() {try {test1.x();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, "a");Thread thread1 = new Thread(new Runnable() {public void run() {try {Test.staticX();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, "b");thread1.start();thread.start();}}


The running result is:
StaticX .......................
X .......................
X .......................
StaticX .......................
StaticX .......................
X .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
StaticX .......................
X .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
X .......................
StaticX .......................


What should we do when we want to synchronize all objects under this class, that is, to share the same lock for all objects under this class?


Check the Code:
package com.jack.zhang.chapter9.classlock;/** *  * @author EX-LIUQI001 *  */public class Test2 {public final static Byte[] locks = new Byte[0];public static void staticX() throws InterruptedException {synchronized (locks) {for (int i = 0; i < 10; i++) {Thread.sleep(1000);System.out.println("staticX.......................");}}}public void x() throws InterruptedException {synchronized (locks) {for (int i = 0; i < 10; i++) {Thread.sleep(1000);System.out.println("x.......................");}}}public static void main(String[] args) {final Test2 test1 = new Test2();Thread thread = new Thread(new Runnable() {public void run() {try {test1.x();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, "a");Thread thread1 = new Thread(new Runnable() {public void run() {try {Test2.staticX();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, "b");thread1.start();thread.start();}}


Running result:


StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................

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.