简体   繁体   English

两种方法(一种同步另一种不同步)和两种线程

[英]Two methods (one synchronized the other not) and two threads

If one method of an object is synchronized and the other isn't, can two different threads run them on the same time? 如果对象的一种方法是同步的,而另一种方法不是同步的,那么两个不同的线程可以同时运行它们吗? I know that when a method is marked synchronized the intrinsic lock has to be acquired, but the second method isn't synchronized, does it matter? 我知道,当某个方法标记为同步时,必须获取内部锁,但是第二种方法不同步,这有关系吗? Or maybe if one thread acquired the lock, all of the methods cannot be entered? 或者,如果一个线程获得了锁,则无法输入所有方法?

Yes. 是。 Two threads can execute those two methods simultaneously. 两个线程可以同时执行这两个方法。 Thread can get lock on synchronized method, but lock is not necessary to execute non-synchronized method. 线程可以锁定同步方法,但执行非同步方法不需要锁定。

I am sure both methods can run concurrently on the same object, because the one which is synchronized will only make use of intrinsic lock of that object & acquire it but as non-synchronized method won't make use of the lock, they both can run concurrently. 我确信这两种方法都可以在同一个对象上同时运行,因为被synchronized将仅使用该对象的内在锁并获取该对象,但是由于非同步方法不会使用该锁,因此它们都可以同时运行。 I came across the same scenario & found the about statement correct. 我遇到了相同的情况并发现about语句正确。 See the below example for more clarity: 请参阅以下示例,以更清楚地了解:

class LockTest {

    // Sync Method. Will acquire the lock
    public synchronized void meth1() {
        System.out.println(Thread.currentThread().getName()
                + " is executing method1.");
        try {
            Thread.sleep(1000); // to show some processing, Won't release the
                                // lock
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out
                .println(Thread.currentThread().getName() + " ended method1.");
    }

    // No lock is required in this method
    public void meth2() {
        try {
            Thread.sleep(1);// to let meth1 execute first & acquire the lock
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()
                + " is executing method2.");
        System.out
                .println(Thread.currentThread().getName() + " ended method2.");
    }

    public static void main(String[] args) throws Exception {
        final LockTest oracle = new LockTest();
        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                oracle.meth1();

            }
        }, "BigThread");

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                oracle.meth2();

            }
        }, "SmallThread");

        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}

The above code produces output as: 上面的代码产生的输出为:

BigThread is executing method1.
SmallThread is executing method2.
SmallThread ended method2.
BigThread ended method1.

The output shows that method2 executed while method1 was also executing concurrently on same object oracle . 输出显示method2method1同时在同一对象oracle上同时执行时执行。

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

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