对象级锁

同步是只适用于方法和块的修饰语,而不适用于类和变量。如果多个线程试图同时对同一个java对象进行操作,那么就有可能出现数据不一致的问题,为了克服这个问题,我们应该使用 “同步 “关键字。如果一个方法或块被声明为同步,那么在同一时间,只允许一个线程在给定的对象上执行该方法或块,这样数据不一致的问题就会得到解决。在内部,同步概念是通过使用锁来实现的。java中的每个对象都有一个唯一的锁。每当我们使用 “synchronized”关键字时,只有锁的概念会出现在画面中。

如果一个线程想在给定的对象上执行一个同步方法,首先它必须获得该对象的锁,一旦线程获得了锁,它就可以在该对象上执行任何同步方法。JVM内部负责获取和释放锁,程序员不负责这项活动。当一个线程在给定对象上执行同步方法时,其余线程不允许在同一对象上同时执行任何同步方法。但其余的线程可以同时执行非同步方法。

对象级锁是一种机制,java中的每个对象都有一个独特的锁,这就是对象级锁。如果一个线程想在给定的对象上执行一个同步方法,那么该线程首先需要对象级锁,一旦该线程获得对象级锁,它就可以在给定的对象上执行任何同步方法,一旦方法执行完毕,线程就会自动释放该对象的锁。

例子:

// Java program to illustrate Object Level Lock Concept

// Import required packages
import java.io.*;
import java.util.*;

// Class 1
class Display {

    // Declaring Non-static wish method
    public void wish(String name)
    {

        // synchronizing wish method
        // and getting the lock of current object
        synchronized (this)
        {

            for (int i = 1; i <= 10; i++) {

                // display message only
                System.out.print("Good Morning: ");

                // Try block to check. for exceptions
                try {

                    // Putting thread on sleep for specified
                    // time
                    // using the sleep() method
                    Thread.sleep(1000);
                }

                // Catch block to handle the exceptions
                catch (InterruptedException e) {

                    // Print the occurred exception/s
                    System.out.println(e);
                }
                // Display message only
                System.out.println(name);
            }
        }
    }
}

// Class 2
class myThread extends Thread {
    // member variable of this class
    Display d;
    String name;

    // Constructor(Parameterized) of this class
    myThread(Display d, String name)
    {

        // this keyword refers to current object itself
        this.d = d;
        this.name = name;
    }

    // run() method for thread
    public void run()
    {

        // Calling wish method of display class
        d.wish(name);
    }
}

// Class 3
// Main Class
class YiibaiDemo {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating display class(Class 1) object
        // in the main() method
        Display d = new Display();

        // Creating thread objects in main method()
        myThread t1 = new myThread(d, "yiibai-1");
        myThread t2 = new myThread(d, "yiibai-2");

        // Starting the threads using the start() method
        t1.start();
        t2.start();
    }
}

类级锁

类级锁是一种机制,java中的每个类都有一个独特的锁,这就是类级锁。如果一个线程想执行一个静态的同步方法,那么这个线程就需要一个类级锁,一旦这个线程得到了类级锁,它就可以执行这个类的任何静态同步方法。一旦方法执行完成,线程会自动释放锁。当一个线程在执行一个静态同步方法时,其余的线程不允许同时执行该类的任何静态同步方法。

例子:

import java.io.*;
import java.util.*;

// Class 1
class Display {

    // Declaring static wish method
    public static void wish(String name)
    {

        // synchronizing wish method
        // and getting the lock of display class
        synchronized (Display.class)
        {

            for (int i = 1; i <= 10; i++) {

                // Display message only
                System.out.print("Good Morning: ");

                // Try block to check for exceptions
                try {
                    // Putting thread on sleep for specified
                    // time
                    // using the sleep() method
                    Thread.sleep(1000);
                }

                // Catch block to handle the exception
                catch (InterruptedException e) {

                    // Throwing exception
                    System.out.println(e);
                }
                // Display message
                System.out.println(name);
            }
        }
    }
}

// Classs 2
class myThread extends Thread {
    // Member variables of this class
    Display d;
    String name;

    // Constructor of this class
    myThread(Display d, String name)
    {

        // This keyword refers to current object itself
        this.d = d;
        this.name = name;
    }

    // run method for thread/s
    public void run()
    {

        // Calling wish method of display class
        d.wish(name);
    }
}

// Class 3
// Main Class
class YiibaiDemo {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating Display class(Class 1) object
        // in the main() method
        Display d = new Display();

        // Creating a thread objects
        myThread t1 = new myThread(d, "yiibai-1");
        myThread t2 = new myThread(d, "yiibai-2");

        // Starting the threads using start() method
        t1.start();
        t2.start();
    }
}

类级锁和对象级锁的区别如下:

类级锁 对象级锁
当想防止多个线程在运行时进入可用实例的同步块时,可以使用这种锁。 当希望代码中的非静态方法或非静态块一次只能被一个线程访问时,就可以使用这种锁。
该锁用于使静态数据成为线程安全的。 该锁用于使非静态数据成为线程安全的。
一个特定类的多个对象可能存在,但总是有一个类的类对象锁可用。 类中的每个对象都有自己的锁。

| ——- | ——- | ——- |

Java中的对象级锁与类级锁

欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果。
转载请注明:文章转载自 有区别网 [http://www.vsdiffer.com]
本文标题:Java中的对象级锁与类级锁
本文链接:https://www.vsdiffer.com/vs/object-level-lock-vs-class-level-lock-in-java.html
免责声明:以上内容仅代表 个人看法、理解、学习笔记、总结和研究收藏。不保证其正确性,因使用而带来的风险与本站无关!如本网站内容冒犯了您的权益,请联系站长,邮箱: ,我们核实并会尽快处理。