java的同步方法和同步代碼塊,對象鎖,類鎖區別

來源:互聯網
上載者:User

標籤:his   static   ati   ring   pos   thread   syn   java   interrupt   

/** * @author admin * @date 2018/1/12 9:48 * 作用在同一個執行個體對象上討論 * synchronized同步方法的測試 * 兩個線程,一個線程調用synchronized修飾方法,另一個線程可以調用非synchronized修飾的方法,互不影響 */public class SynchronizedTest {    public synchronized void methodA() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodA-" + i);                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public void methodB() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodB-" + i );                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        SynchronizedTest test = new SynchronizedTest();        Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {                test.methodA();            }        });        thread1.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                test.methodB();            }        });        thread2.start();    }}運行結果:methodA-0methodB-0methodA-1methodB-1methodB-2methodA-2methodA-3methodB-3methodA-4methodB-4

 

/** * @author admin * @date 2018/1/12 10:16 * 作用在同一個執行個體對象上討論 * Sychronized代碼塊的測試 * 兩個線程,一個線程執行synchronized代碼塊,另一個線程執行非synchronized代碼塊 */public class SychronizedTest2 {    public void methodA() {        synchronized (this) {            try {                for (int i = 0; i < 5; i++) {                    System.out.println("methodA-" + i);                    Thread.sleep(1000);                }            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    public void methodB() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodB-" + i);                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        SychronizedTest2 test2 = new SychronizedTest2();        Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {               test2.methodA();            }        });        thread1.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                test2.methodB();            }        });        thread2.start();    }}運行結果:methodA-0methodB-0methodA-1methodB-1methodA-2methodB-2methodB-3methodA-3methodA-4methodB-4

 

/** * @author admin * @date 2018/1/12 10:33 * 作用在同一個執行個體對象上討論 * Synchronized同步方法和同步代碼塊 * 1、synchronized和synchronized(this)二者沒區別,都作用在this對象鎖上面,所以會同步 * 2、synchronized(obj),這個是作用在obj對象鎖上面,和this對象鎖不同,所以不會同步 */public class SynchronizedTest3 {    public synchronized void methodA() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodA-" + i);                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public void methodB() {        synchronized (this) {            try {                for (int i = 0; i < 5; i++) {                    System.out.println("methodB-" + i);                    Thread.sleep(1000);                }            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    public void methodC() {        Object obj = new Object();        synchronized (obj) {            try {                for (int i = 0; i < 5; i++) {                    System.out.println("methodC-" + i);                    Thread.sleep(1000);                }            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    public static void main(String[] args) {        SynchronizedTest3 test3 = new SynchronizedTest3();        Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {                test3.methodA();            }        });        thread1.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                test3.methodB();            }        });        thread2.start();        Thread thread3 = new Thread(new Runnable() {            @Override            public void run() {                test3.methodC();            }        });        thread3.start();    }}運行結果:methodA-0methodC-0methodA-1methodC-1methodA-2methodC-2methodA-3methodC-3methodA-4methodC-4methodB-0methodB-1methodB-2methodB-3methodB-4

 

/** * @author admin * @date 2018/1/12 10:48 * 作用在同一個類上討論,每一個類只有一個類鎖 * synchronized類鎖 * static synchronized 和 synchronized(SynchronizedTest4.class),都是作用在同一個類鎖上,所以會同步 */public class SynchronizedTest4 {    public synchronized static void methodA() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodA-" + i);                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public void methodB() {        synchronized (SynchronizedTest4.class) {            try {                for (int i = 0; i < 5; i++) {                    System.out.println("methodB-" + i);                    Thread.sleep(1000);                }            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    public static void main(String[] args) {        SynchronizedTest4 test4 = new SynchronizedTest4();        Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {                test4.methodA();            }        });        thread1.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                test4.methodB();            }        });        thread2.start();    }}運行結果:methodA-0methodA-1methodA-2methodA-3methodA-4methodB-0methodB-1methodB-2methodB-3methodB-4

 

/** * @author admin * @date 2018/1/12 11:03 * synchronized的對象鎖和static synchronized的類鎖,是兩個不同的鎖,所以不會同步 * 兩個線程,一個調用對象鎖,一個調用類鎖 */public class SynchronizedTest5 {    public synchronized  void methodA() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodA-" + i);                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public synchronized static void methodB() {        try {            for (int i = 0; i < 5; i++) {                System.out.println("methodB-" + i);                Thread.sleep(1000);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        SynchronizedTest5 test5 = new SynchronizedTest5();        Thread thread1 = new Thread(new Runnable() {            @Override            public void run() {                test5.methodA();            }        });        thread1.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                test5.methodB();            }        });        thread2.start();    }}運行結果:methodA-0methodB-0methodA-1methodB-1methodB-2methodA-2methodB-3methodA-3methodB-4methodA-4

 

java的同步方法和同步代碼塊,對象鎖,類鎖區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.