简体   繁体   English

锁定静态对象

[英]Locking on static object

There are multiple instance of Class A that runs at a time. 一次运行多个A类实例。

Class A calls multiple instances of Class B in its run. A类在其运行中调用B类的多个实例。

public Class Main {

    public static void main(String args[] ) {

        A a1 = new A();
        Thread t1 = new Thread(a1);
        t1.start();

        A a2 = new A();
        Thread t2 = new Thread(a2);
        t2.start();
    }
}

Class A implements Runnable {

    public void run() {

        B b1 = new B();
        Thread t11 = new Thread(b1);
        t11.start();

        B b2 = new B();
        Thread t21 = new Thread(b2);
        t21.start();
    }
}

There is method named "method" in class B where a Set Collection is edited. B类中有一个名为“方法”的方法,在其中编辑Set Collection。 That edit is done based on static lock in Class B. 该编辑是基于类B中的静态锁定完成的。

EDIT - 编辑 -

Class B implements Runnable {

    private final static Object LOCK = new Object();
    private final static Set<T> busyRecords = new HashSet<T>();

    public void waitToWorkOn(final T obj) {
        synchronized(LOCK) {
            while (busyRecords.contains(obj)) {
        LOCK.wait(); //go to sleep
       }
       busyRecords.add(obj);            
    }
    }

    public void doneWith(final T obj) {
        synchronized(LOCK) {
           busyRecords.remove(obj);
      LOCK.notifyAll(); 
    }
    }

    public void mathod(obj)  {

     try{
        waitToWorkOn(obj);

         .. do some work with obj
     }
     finally {
        doneWith(obj);
     }
    }

    public void run() {
        method(getObj())
    }
}

But that Set does not need concurrency control when it is accessed from different "A" instances. 但是,集不需要并发控制,当它从不同的“A”的实例访问。 Only within an A instance, it needs to be locked for all B instances . 仅在A实例内,才需要为所有B实例锁定它

By this I mean, that when 2 instances of A are running, they should not be made to wait. 我的意思是,当A的2个实例在运行时,不应使其等待。 But within an A instance if 2 B objects pick same obj, they have to wait inside LOCK.wait. 但是在一个A实例中,如果2个B对象选择相同的obj,则它们必须在LOCK.wait内部等待。

I don't think that LOCK can be made non-static as A calls multiple instances of B.Can we tune LOCK object here for better concurrency across A objects. 我不认为LOCK可以变成非静态的,因为A调用了B的多个实例。我们可以在这里调整LOCK对象以获得更好的并发性吗?

You can create a thread-safe instance of the shared collection and pass it to all the Bs for a given A. 您可以创建共享集合的线程安全实例,并将其传递给给定A的所有B。

Class A implements Runnable {

    public void run() {

        // create shared set instance scoped to A, and make it thread-safe
        Set col = Collections.synchronizedSet(new HashSet());

        B b1 = new B(col);
        Thread t11 = new Thread(b1);
        t11.start();

        B b2 = new B(col);
        Thread t21 = new Thread(b2);
        t21.start();
    }
}

Class B implements Runnable {

    private final Set<T> someSet;

    private B(Set<T> someSet) {
      this.someSet = someSet;
    }

    public void method(final T obj) {
        someSet.add(obj);
    }

    public void run() {
        method()
    }
}

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

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