Java synchronization Mechanism object lock usage Comparison

Source: Internet
Author: User


Class Sync {private byte[] lock = new Byte[0];        public void Sync () throws Interruptedexception {synchronized (lock) {runthread ();        }} public void Thissync () throws Interruptedexception {synchronized (this) {runthread ();     }} public synchronized static void Staticsync () throws Interruptedexception {//synchronous static function Runthread (); } public void Classsync () throws Interruptedexception {//synchronized (Sync.class) {Runthread (        );        }} private static void Runthread () throws Interruptedexception {Thread current = Thread.CurrentThread ();        System.out.println ("Current thread ID:" + current.getid () + "Enter ...");        System.out.println ("1111");        Thread.Sleep (1000);        System.out.println ("2222");        Thread.Sleep (1000);        System.out.println ("3333");        Thread.Sleep (1000);        System.out.println ("4444");        Thread.Sleep (1000); System.Out.println ("5555");    System.out.println ("Current thread ID:" + current.getid () + "out ..."); }}




1. Whether the same thread lock object locks are reusable (that is, do not wait)

CONCLUSION: REUSABLE

public class Testobjectsynclock {    private static byte[] lock = new byte[0];//0 Length byte array object will be created more economically than any object. View compiled bytecode: Generate 0-length byte[] objects with only 3 opcode, and object lock = new Object () requires 7 lines of opcode. Public     static void Main (string[] args) throws Interruptedexception {    //1. The same thread lock object locks are reusable (that is, do not wait)----REUSABLE                synchronized (lock) {            new Thread (new Runnable () {public                void run () {                    try {                        thread.sleep ()};                    } catch (Interruptedexception e) {                        e.printstacktrace ();                    }                    System.out.println ("AAA");                }            ). Start ();            Lock.notify ();        }        Synchronized (lock) {            System.out.println ("BBB");            Lock.wait ();}}}   

Results:

Bbb
Aaa

2. Same object, multi-threaded access (one main thread, one new thread)

Conclusion: The lock is effective

  Thread current = Thread.CurrentThread ();         System.out.println ("Current thread ID:" +current.getid () + "start ...");       Final sync Sync =new sync ();               New Thread (New Runnable () {public                        void run () {                Thread current = Thread.CurrentThread ();                  System.out.println ("Current thread ID:" +current.getid () + "start ...");                try {                    sync.sync ();                } catch (Interruptedexception e) {                    e.printstacktrace ();}}}        ). Start ();                Sync.sync ();

Results:

Current thread Id:1start ...
Current thread Id:1enter ...
1111
Current thread Id:9start ...
2222
3333
4444
5555
Current thread id:1out ...
Current thread Id:9enter ...
1111
2222
3333
4444
5555
Current thread id:9out ...


3. Different objects, multi-threaded access

Conclusion: Lock is invalid


Thread current = Thread.CurrentThread ();        System.out.println ("Currret thread ID:" + current.getid () + "start ...");        Final Sync sync = new sync ();        Final Sync sync2 = new sync ();        New Thread (New Runnable () {public            void run () {                Thread current = Thread.CurrentThread ();                System.out.println ("Currret thread ID:" + current.getid () + "start ...");                try {                    sync2.sync ();                } catch (Interruptedexception e) {                    e.printstacktrace ();}}}        ). Start ();        Sync.sync ();

Results:

Currret Thread Id:1start ...
Current thread Id:1enter ...
1111
Currret Thread Id:9start ...
Current thread Id:9enter ...
1111
2222
2222
3333
3333
4444
4444
5555
Current thread id:1out ...
5555
Current thread id:9out ...


4.wait (), notify ()----call wait or notify, the thread must be the owner of the lock on the object, or it will throw a exception in thread "Thread-0" Java.lang.IllegalMonitorStateException

New Thread (New Runnable () {public            void run () {                System.out.println ("------");                Synchronized (lock) {                    try {                        thread.sleep],                    } catch (Interruptedexception e) {                        e.printstacktrace ( );                    }                    Lock.notify ();                    System.out.println ("After notify ...");}}        ). Start ();        Synchronized (lock) {            System.out.println ("Enter ...");            Lock.wait ();            System.out.println ("Out ...");        

Results:

Enter ...
------
After notify ...
Out ...


5.wait (), Notifyall ()----notifyall () wakes up all the different threads in wait

    New Thread (New Runnable () {public void run () {synchronized (lock) {Sys                    Tem.out.println ("Enter thread 1 ...");                    try {lock.wait ();                    } catch (Interruptedexception e) {e.printstacktrace ();                } System.out.println ("Leave thread 1 ...");        }}). Start (); New Thread (New Runnable () {public void run () {synchronized (lock) {System.                    Out.println ("Enter thread 2 ...");                    try {lock.wait ();                    } catch (Interruptedexception e) {e.printstacktrace ();                } System.out.println ("Leave thread 2 ...");        }}). Start (); New Thread (New Runnable () {public void run () {System.out.priNTLN ("------");                    Synchronized (lock) {try {thread.sleep (3000);                    } catch (Interruptedexception e) {e.printstacktrace ();                    } lock.notifyall ();//The Notifyall method wakes all threads that are waiting for control of the object.                Lock.notify ();//randomly notifies a waiting thread System.out.println ("after notifyall ..."); }}). Start ();

Results:

Enter Thread 1 ...
Enter Thread 2 ...
------
After Notifyall ...
Leave thread 2 ...
Leave thread 1 ...

6.static synchronization methods, different objects

Thread current = Thread.CurrentThread ();        System.out.println ("Currret thread ID:" + current.getid () + "start ...");        Sync sync = new sync ();        New Thread (New Runnable () {public            void run () {                sync sync = new sync ();                Thread current = Thread.CurrentThread ();                System.out.println ("Currret thread ID:" + current.getid () + "start ...");                try {                    sync.staticsync ();//Lock valid/                    /Sync.staticsync ();//Lock valid                    //Sync.sync ();//Lock Invalid                } catch ( Interruptedexception e) {                    e.printstacktrace ();}}}        ). Start ();        Sync.staticsync ();//Lock valid        //Sync.staticsync ();//Lock valid        //Sync.sync ();//Lock invalid

Results:

Currret Thread Id:1start ...
Current thread Id:1enter ...
1111
Currret Thread Id:9start ...
2222
3333
4444
5555
Current thread id:1out ...
Current thread Id:9enter ...
1111
2222
3333
4444
5555
Current thread id:9out ...

7.class synchronization methods, different objects


Thread current = Thread.CurrentThread ();        System.out.println ("Currret thread ID:" + current.getid () + "start ...");        Sync sync = new sync ();        New Thread (New Runnable () {public            void run () {                sync sync = new sync ();                Thread current = Thread.CurrentThread ();                System.out.println ("Currret thread ID:" + current.getid () + "start ...");                try {                    sync.classsync ();//Lock valid                } catch (Interruptedexception e) {                    e.printstacktrace ();}}}        ). Start ();        Sync.classsync ();//Lock valid

Results:

Currret Thread Id:1start ...
Current thread Id:1enter ...
1111
Currret Thread Id:9start ...
2222
3333
4444
5555
Current thread id:1out ...
Current thread Id:9enter ...
1111
2222
3333
4444
5555
Current thread id:9out ...

8.this synchronous method, different objects, like 2, Object lock

Final Sync sync = new sync ();                New Thread (New Runnable () {public void run () {//sync Sync =new sync ();                Thread current = Thread.CurrentThread ();                System.out.println ("Currret thread ID:" + current.getid () + "start ...");                try {sync.thissync ();                } catch (Interruptedexception e) {e.printstacktrace ();        }}). Start ();                New Thread (New Runnable () {public void run () {//sync Sync =new sync ();                Thread current = Thread.CurrentThread ();                System.out.println ("Currret thread ID:" + current.getid () + "start ...");                try {sync.thissync ();                } catch (Interruptedexception e) {e.printstacktrace (); }}). Start ();

Results:

Currret Thread Id:9start ...
Current thread Id:9enter ...
1111
Currret Thread Id:10start ...
2222
3333
4444
5555
Current thread id:9out ...
Current thread Id:10enter ...
1111
2222
3333
4444
5555
Current thread id:10out ...






Java synchronization Mechanism object lock usage Comparison

Related Article

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.