简体   繁体   English

Java多线程非静态和静态方法的行为保持不变

[英]Java Multi threading Non Static and Static method behavior remains same

When I synchronize non static method and static method the behavior remains the same. 当我同步非静态方法和静态方法时,行为保持不变。 Thread locks the instance for both static and non staitc methods when i put synchronized(Task.class) in both the cases 在两种情况下,当我将synced(Task.class)都放入时,线程会同时为静态和非staitc方法锁定实例

public class ThreadDemo {
    public static void main(String[] args) {

        for(int i=0;i<10;i++){
            Task task =new Task();
            new Thread(task).start();
        }
    }
}
class Task implements Runnable {

    @Override
    public void run() {
        printThreadName();

    }

    public  void printThreadName() {
        synchronized (Task.class) {


        System.out.println("Starting-->"+Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Ending->"+Thread.currentThread().getName());
        }
    }

}

and after making the method static 在将方法设为静态之后

public  static  void printThreadName() {
        synchronized (Task.class) {


        System.out.println("Starting-->"+Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Ending->"+Thread.currentThread().getName());
        }
    }

Every Thread locks this class until it completes and so on. 每个线程都会锁定该类,直到完成为止,依此类推。 My question is why this behavior is same even for non static method in the first case. 我的问题是为什么在第一种情况下,即使对于非静态方法,此行为也是如此。

I'll summarize the comments above: 我将总结以上评论:

The JLS states the following: (emphasis by me) JLS声明以下内容:(我强调)

If the method is an instance method , it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). 如果该方法是实例方法 ,则它将锁定与其调用实例相关联的监视器(即,在该方法的主体执行期间将被称为此对象的对象)。 If the method is static , it locks the monitor associated with the Class object that represents the class in which the method is defined. 如果该方法是static ,则它将锁定与Class对象关联的监视器,该对象代表定义该方法的类。

So this will synchronize on the instance: 因此,这将在实例上同步:

public synchronized void someMethod() { ... }

This will synchronize on the class: 这将在类上同步:

public static synchronized void someMethod() { ... }

When you use a synchronized block inside a method you need to provide the monitor which can be a class (eg Myclass.class ) or any object instance (which could be this ): 当您方法内部使用同步块时,您需要提供监视器,该监视器可以是类(例如Myclass.class )或任何对象实例(可以是this ):

synchronized(monitor) { ... }

You could use synchronized(getClass()) or synchronized(this) to get behavior very similar to method level synchronization (I'm not sure whether the getClass() variant would behave the same in all cases). 您可以使用synchronized(getClass())synchronized(this)来获得与方法级同步非常相似的行为(我不确定getClass()变体在所有情况下的行为是否相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM