A good memory is better than a bad pen. 78-Multithreading-non-static synchronized influence range

Source: Internet
Author: User

The difference between synchronized and static synchronized

Synchronized is a lock on the current instance of the class, preventing other threads from accessing all the synchronized blocks of that instance of the class at the same time, note that this is the current instance of the class, and there is no such constraint for the two different instances of the class. then static synchronized happens to control access to all instances of the class , and static synchronized is a fast way to restrict all instances of the class in the JVM from accessing the corresponding code at the same time. In fact, there is a synchronized in a method or block of code in a class, and after generating an instance of that class, the class will have a monitor fast, the placement thread concurrently accesses the instance synchronized protection fast, and the static Synchronized is the common one for all instances of this class A monitoring is fast, that is, the difference of two, that is, synchronized equivalent to this.synchronized, and static Synchronized equivalent to something.synchronized.
  
non-static synchronized have a very small impact and can only be accessed synchronously within the thread of the same object, and the threads of different objects cannot be accessed synchronously.
Like what:
Easyselftsynchthread thread1 = new Easyselftsynchthread ();
Same Thread Object
thread T1 = new Thread (thread1, "T1");
Thread t2 = new Thread (thread1, "==t2");//
The corresponding Run method is:
Public synchronized void Run () {
The result of the run is synchronous access.

If this is the following:
Easyselftsynchthread thread1 = new Easyselftsynchthread ();
Easyselftsynchthread thread2 = new Easyselftsynchthread ();
2 Thread objects
thread T1 = new Thread (thread1, "T1");
Thread t2 = new Thread (thread2, "==t2");//(3)
Then the Run method, in T1 and T2, will not be accessed synchronously.

Experience the impact of non-static synchronized Java source code

 PackageCom.thread;/** * Non-static synchronized the scope of the impact is very small, can only synchronize access to the same object's internal thread, the different object's thread cannot synchronously access * * @author Fan Fangming */  Public  class easyselftsynchthread implements Runnable {     Public void Print() { for(inti =0; I <3; i++) {System.out.println (Thread.CurrentThread (). GetName () +" : "+ i);Try{Thread.Sleep ( -); }Catch(Exception e)            {E.printstacktrace (); }        }    }//public void Run () {//(1)     Public synchronized void Run() { This. print (); } Public Static void Main(string[] args) {Easyselftsynchthread Thread1 =NewEasyselftsynchthread (); Easyselftsynchthread thread2 =NewEasyselftsynchthread ();//Same Thread objectThread T1 =NewThread (Thread1,"T1"); Thread t2 =NewThread (Thread1,"==t2");//(2)        //2 different thread objects        //thread t2 = new Thread (thread2, "==t2");//(3)T1.start ();    T2.start (); }}

Run results
The result of running the same thread object:
t1:0
T1:1
T1:2
==t2:0
==t2:1
==t2:2

Turn (2) The comment and open the note (3).
The results of the operation are as follows:
==t2:0
t1:0
==t2:1
T1:1
==t2:2
T1:2

Although we declare the run () function as synchronized in code (1), because T1, T2 is a thread of two objects (R1, r2), and the run () function is non-static synchronized data, So it can still be accessed concurrently (the Sleep () function in the code (2) does not release the "flag lock" because it is paused, because loops in the thread are difficult to break to execute another thread.

A good memory is better than a bad pen. 78-Multithreading-non-static synchronized influence range

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.