Java中同步语句的静态锁定对象

时间:2013-02-19 09:35:14

标签: java multithreading synchronization

  

请不要陷入这样的陷阱,认为你可以锁定A类的任意实例,并以某种方式锁定另一个A类实例。这是一个经典的初学者的错误。

在我理解之前,我犯了几次错误。但静态锁定对象可以正常工作。

MyThread的

package com.replanet;

public class MyThread extends Thread {

    private int x, y;
    private static Object lock3 = new Object();

    public MyThread(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void run() {
        super.run();
        try {
            test_Method();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private void test_Method() throws InterruptedException {
        synchronized (lock3) {
            System.out.println("test_Method " + Thread.currentThread().getName());
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                if (i == Integer.MAX_VALUE / 2) {
                    Thread.sleep(2000);
                    System.out
                            .println("Leaving test_Method on " + Thread.currentThread().getName());
                    return;
                }
            }
        }
    }

}

用法

package com.replanet;

public class Main {

    public static void main(String[] args) {

        MyThread myThread1 = new MyThread(1, 2);
        MyThread myThread2 = new MyThread(1, 2);
        myThread1.start();
        myThread2.start();
    }
}

输出

test_Method Thread-0
Leaving test_Method on Thread-0
test_Method Thread-1
Leaving test_Method on Thread-1

使用非静态锁定对象输出(不适合我)

test_Method Thread-0
test_Method Thread-1
Leaving test_Method on Thread-1
Leaving test_Method on Thread-0

使用static锁定对象是个好主意吗?

3 个答案:

答案 0 :(得分:3)

你可以锁定Class本身 - 这更有意义,更容易阅读:

private void test_Method() throws InterruptedException {
        synchronized (MyThread.class) {
            System.out.println("test_Method " + Thread.currentThread().getName());
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                if (i == Integer.MAX_VALUE / 2) {
                    Thread.sleep(2000);
                    System.out
                            .println("Leaving test_Method in " + Thread.currentThread().getName());
                    return;
                }
            }
        }
    }

或者,如果您不需要该方法作为实例方法:

private static synchronized void test_Method() throws InterruptedException {
            System.out.println("test_Method " + Thread.currentThread().getName());
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                if (i == Integer.MAX_VALUE / 2) {
                    Thread.sleep(2000);
                    System.out
                            .println("Leaving test_Method in " +  Thread.currentThread().getName());
                    return;
                }
            }
    }

您可能还想阅读新的(ish)Lock课程。

答案 1 :(得分:1)

而不是静态让MyThread共享一个锁定对象会更好。更面向对象。

答案 2 :(得分:0)

它是静态成员还是实例成员取决于你希望它具有的范围,但是 一个好的做法是拥有私有对象来锁定。这是锁定(显然是公共的)类对象的主要优势。

相关问题