java synchronized(object/this)的 區別

來源:互聯網
上載者:User

標籤:執行個體   read   stat   log   ack   todo   catch   目的   name   

1.synchronized(object)

package test.thread;import java.io.IOException;import org.junit.Test;/* * 測試線程鎖 */public class TestBlock {              public static void main(String[] args)  {            TestBlock test = new TestBlock();            MyTest thread1 = test.new MyTest(test);            thread1.setName("1");            MyTest thread2 = test.new MyTest(test);            thread2.setName("2");            thread1.start();            thread2.start();        }      /*     * 測試同步     */    class MyTest extends Thread{        private Object o;                public  MyTest(Object o){            this.o=o;        }                @Override        public void run() {            // TODO Auto-generated method stub            synchronized (o) {  //這個o是test對象的執行個體 ,對類對象執行個體進行加鎖,當線程調用一個執行個體啟動並執行,另外的線程調用這個執行個體時候阻塞,達到上鎖的目的                try {                    for(int a=0;a<3;a++){                        System.out.println("線程"+MyTest.currentThread().getName()+"修改a=="+a);                        //MyTest.yield();                    }                } catch (Exception e) {                    // TODO: handle exception                }                            }        }            }        }

返回的結果:
  ·    
  

     

線程1修改a==0
線程1修改a==1
線程1修改a==2
線程1修改a==3
線程1修改a==4
線程1修改a==5
線程1修改a==6
線程1修改a==7
線程1修改a==8
線程1修改a==9
線程2修改a==0
線程2修改a==1
線程2修改a==2
線程2修改a==3
線程2修改a==4
線程2修改a==5
線程2修改a==6
線程2修改a==7
線程2修改a==8
線程2修改a==9

可以看到當一個線程運行完畢之後才運行第二個線程

2.synchronized(this)

package test.thread;import java.io.IOException;import org.junit.Test;/* * 測試線程鎖 */public class TestBlock {              public static void main(String[] args)  {            TestBlock test = new TestBlock();            MyTest thread1 = test.new MyTest(test);            thread1.setName("1");            MyTest thread2 = test.new MyTest(test);            thread2.setName("2");            thread1.start();            thread2.start();        }      /*     * 測試同步     */    class MyTest extends Thread{        private Object o;                public  MyTest(Object o){            this.o=o;        }                @Override        public void run() {            // TODO Auto-generated method stub            synchronized (this) {                try {                    for(int a=0;a<10;a++){                        System.out.println("線程"+MyTest.currentThread().getName()+"修改a=="+a);                        //MyTest.yield();                    }                } catch (Exception e) {                    // TODO: handle exception                }                            }        }            }        }

返回的結果:

線程1修改a==0
線程1修改a==1
線程1修改a==2
線程1修改a==3
線程1修改a==4
線程2修改a==0
線程1修改a==5
線程1修改a==6
線程1修改a==7
線程1修改a==8
線程1修改a==9
線程2修改a==1
線程2修改a==2
線程2修改a==3
線程2修改a==4
線程2修改a==5
線程2修改a==6
線程2修改a==7
線程2修改a==8
線程2修改a==9

 

 

 

 

java synchronized(object/this)的 區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.