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


/**
 * @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-0
methodB-0
methodA-1
methodB-1
methodB-2
methodA-2
methodA-3
methodB-3
methodA-4
methodB-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-0
methodB-0
methodA-1
methodB-1
methodA-2
methodB-2
methodB-3
methodA-3
methodA-4
methodB-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-0
methodC-0
methodA-1
methodC-1
methodA-2
methodC-2
methodA-3
methodC-3
methodA-4
methodC-4
methodB-0
methodB-1
methodB-2
methodB-3
methodB-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-0
methodA-1
methodA-2
methodA-3
methodA-4
methodB-0
methodB-1
methodB-2
methodB-3
methodB-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-0
methodB-0
methodA-1
methodB-1
methodB-2
methodA-2
methodB-3
methodA-3
methodB-4
methodA-4

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM