静的メソッドと非静的メソッドクラスロックとオブジェクトロックについて

4008 ワード

重点を置く
staticメソッドはクラス内のメンバーメソッドで、クラス全体に属し、オブジェクトを作成しなくても直接呼び出すことができます.
静的メソッドはインスタンス化よりも効率的であり,静的メソッドの欠点は自動的に破棄されず,インスタンス化されたメソッドは破棄できることである.
静的メソッドと静的変数は、作成後も常に同じメモリを使用しますが、インスタンスを使用すると複数のメモリが作成されます.
 
 
静的メソッドまたはメソッドブロックに追加されたロックはクラスロック(ロッククラス)である
非静的メソッドはオブジェクトロック(オブジェクトロック)
 
詳細は以下の通り  
クラスロック(ロッククラス):
public class StaticTest {

    public synchronized static void fun1() throws InterruptedException {
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                System.out.println("Static fun1 is running...");
            }
    }

    public synchronized static void fun2() throws InterruptedException {
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                System.out.println("Static fun2 is running...");
            }
    }

    public static void main(String[] args) {
        StaticTest staticTest1 = new StaticTest();
        Thread t1 = new Thread(() -> {
            try {
                StaticTest.fun1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        StaticTest staticTest2 = new StaticTest();
        Thread t2 = new Thread(() -> {
            try {
                StaticTest.fun2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();t2.start();
    }
}

発見されます
ずっとfunを コンテンツがすべて実行されてから、別のfunのコンテンツが実行されます.
 
 
オブジェクトロック:
 
public class StaticTest {

    public synchronized  void fun1() throws InterruptedException {
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                System.out.println("Static fun1 is running...");
            }
    }

    public synchronized  void fun2() throws InterruptedException {
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                System.out.println("Static fun2 is running...");
            }
    }

    public static void main(String[] args) {
        StaticTest staticTest1 = new StaticTest();
        Thread t1 = new Thread(() -> {
            try {
                staticTest1.fun1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        StaticTest staticTest2 = new StaticTest();
        Thread t2 = new Thread(() -> {
            try {
                staticTest2.fun2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();t2.start();
    }
}

異なるオブジェクトのfunメソッド  
交互印刷がロックされていません
 
public class StaticTest {

    public synchronized  void fun1() throws InterruptedException {
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                System.out.println("Static fun1 is running...");
            }
    }

    public synchronized  void fun2() throws InterruptedException {
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                System.out.println("Static fun2 is running...");
            }
    }

    public static void main(String[] args) {
        StaticTest staticTest1 = new StaticTest();
        Thread t1 = new Thread(() -> {
            try {
                staticTest1.fun1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        StaticTest staticTest2 = new StaticTest();
        Thread t2 = new Thread(() -> {
            try {
                staticTest1.fun2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();t2.start();
    }
}

 
発見されます
ずっとfunを コンテンツがすべて実行されてから、別のfunのコンテンツが実行されます.  オブジェクトをロックしました