Thread static synchronization vs. non-static differences

Source: Internet
Author: User

So what's the difference between static and non-static methods before adding synchronized?


As we all know, the static method belongs to the class method, it belongs to this class (note: The class here does not mean a specific pair of class
), then the lock acquired by Static is the class (class) of the object that is currently calling this method, and is no longer generated by this class
of a specific object). Instead of the lock acquired by the static method, it is the lock of the object that is currently calling this method. So they don't
will result in mutual exclusion.
package com.jack.zhang.chapter9.classlock;/** * * @author ex-liuqi001 * */public class Test {public static synchronized void STA Ticx () throws interruptedexception {for (int i = 0; i <; i++) {thread.sleep (1000); System.out.println ("Staticx ..................) public synchronized void X () throws interruptedexception {for (int i = 0; i < x 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 Aut O-generated catch Blocke.printstacktrace ();}}}, "a"); Thread thread1 = new Thread (new Runnable () {public void run () {try {test.staticx ()}, catch (Interruptedexception e) {//To Do auto-generated catch Blocke.printstacktrace ();}}, "B"); Thread1.start (); Thread.Start ();}} 


The operating result is:
Staticx ........ .........
X.......................
X.......................
Staticx ........ .........
Staticx ........ .........
X.......................
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
Staticx ........ .........
X.......................
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........


What do we do when we want all the objects under this class to be synchronized, that is, to have all the objects under this class share the same lock?


Look at the code:
Package com.jack.zhang.chapter9.classlock;/** * * @author ex-liuqi001 * */public class Test2 {public final static byte[] Locks = new Byte[0];p ublic static void Staticx () throws Interruptedexception {synchronized (locks) {for (int i = 0; I &lt ; 10; i++) {thread.sleep (1000); System.out.println ("Staticx ..................) public void X () throws Interruptedexception {synchronized (locks) {for (int i = 0; i <; 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 Aut O-generated catch Blocke.printstacktrace ();}}}, "a"); Thread thread1 = new Thread (new Runnable () {public void run () {try {test2.staticx ()} catch (Interruptedexception e) {//T ODO auto-generated catch Blocke.printstacktrace ();}}, "B"); Thread1.start (); Thread.Start ();}}


Operation Result:


Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................

Thread static synchronization vs. non-static 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.