简体   繁体   English

Java中的MultiTheading具有静态和非静态方法

[英]MultiTheading in Java having static and non static method

I had been trying out different behavior of multi-threading in java. 我一直在尝试java中的多线程的不同行为。 If i am using both synchronized static and non static method in a class. 如果我在类中使用同步静态和非静态方法。 What i had understood is, 我所理解的是,

-> if the thread enters into a synchronize method the thread acquire the lock of an object until the execution of method. - >如果线程进入同步方法,则线程获取对象的锁定,直到执行方法。

-> if the thread enters into a static synchronize method the thread acquire the lock of a class instead of an object. - >如果线程进入静态同步方法,则线程获取类的锁而不是对象。

The Real confusion part is the output ??. Real confusion部分是输出??。

    package com.threadImplementaion.examples;

class MyRunable implements Runnable
{
    public void run()
    {
        iterationMethod() ;
        staticIteration();
    }
    public synchronized void iterationMethod() 
    {
        //int count = 0  ;
        for(int i = 0 ; i < 5; i++ )
        {
            System.out.println( Thread.currentThread().getName() + " : " +  i);
        }
    }
    public  static synchronized  void staticIteration()
    {
        for(int i = 0 ; i < 10 ; i++ )
        {
            System.out.println(Thread.currentThread().getName() +  " static "  +  i);
        }
    }
}
public class MyRunnable 
{
    public static void main(String[] args) {
        Runnable runnable = new MyRunable() ;
        Thread thread1 = new Thread(runnable);
        thread1.start();
        thread1.setName("Thread1");
        Thread thread2 = new Thread(runnable) ;
        thread2.start();
        thread2.setName("Thread2") ;
        Thread thread3 = new Thread(runnable); 
        thread3.start();
        thread3.setName("Thread3");
        }


}



Output :
Thread1 : 0
Thread1 : 1
Thread1 : 2
Thread1 : 3
Thread1 : 4
Thread1 static 0
Thread1 static 1
Thread1 static 2
Thread1 static 3
Thread1 static 4
Thread1 static 5
**Thread1 static 6**
Thread3 : 0
Thread3 : 1
Thread3 : 2
Thread3 : 3
Thread3 : 4
Thread2 : 0
Thread2 : 1
Thread1 static 7
Thread2 : 2
Thread1 static 8
Thread2 : 3
Thread1 static 9
Thread2 : 4
Thread2 static 0
Thread2 static 1
Thread2 static 2
Thread2 static 3
Thread2 static 4
Thread2 static 5
Thread2 static 6
Thread2 static 7
Thread2 static 8
Thread2 static 9
Thread3 static 0
Thread3 static 1
Thread3 static 2
Thread3 static 3
Thread3 static 4
Thread3 static 5
Thread3 static 6
Thread3 static 7
Thread3 static 8
Thread3 static 9

A synchronized static method obtains a lock on the Class object X representing the class in which the method is defined. 同步static方法获取Class对象X上的锁定,该对象X表示定义方法的类。 In this case, the synchronized keyword is in principle meant to synchronize between static methods only. 在这种情况下, synchronized关键字原则上意味着仅在static方法之间进行同步。

Whereas a synchronized instance (non- static ) method locks the current object Y on which the method is called. 而同步实例(非static )方法锁定调用该方法的当前对象Y

Therefore, a synchronized static method and a synchronized instance method can still interleave because they lock on two different objects. 因此,同步static方法和同步实例方法仍然可以交错,因为它们锁定了两个不同的对象。

static methods lock/unlock the class( MyRunable ) object where as non static methods lock/unock the MyRunable objects. static方法锁定/解锁类( MyRunable )对象,其中非静态方法锁定/取消MyRunable对象。 And this both are different, I mean to say. 我的意思是,这两者都不同。

A synchronized method (§ 8.4.3.6 ) automatically performs a lock action when it is invoked; 同步方法(第8.4.3.6节)在调用时自动执行锁定操作; its body is not executed until the lock action has successfully completed. 在锁定操作成功完成之前,它的主体不会执行。

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). 如果该方法是实例方法,则它会锁定与调用它的实例关联的监视器(即,在执行方法主体期间将被称为this的对象)。

If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined. 如果方法是静态的,它将锁定与Class对象关联的监视器,该对象表示定义方法的类。

If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor. 如果方法的主体的执行正常或突然完成,则会在同一监视器上自动执行解锁操作。

Link 链接

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

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