简体   繁体   English

JAVA方法中的互斥线程

[英]Mutual thread exclusion in a JAVA method

I need to provide mutual access to a method of a class, so one thread at time can execute the code inside the method. 我需要提供对类方法的相互访问,因此,一个线程可以同时执行该方法中的代码。 I have tried the solution below but I cannot understand why every time the value of the variable 'locked' is false. 我已经尝试过以下解决方案,但是我无法理解为什么每次“锁定”变量的值都是false时,为什么。

public class MyCommand extends BaseCommand {
  private static boolean locked=false;

  @Override
  public  boolean execute() throws Exception {
    synchronized(MyCommand.class){
        while (locked){
            wait();
        }
        locked=true;

         //some code with a breakpoint/long blocking operations

        locked=false;
        notifyAll();
        return true;
  }
}

I have placed a breakpoint in the while statement at the beginning and the value of the variable locked is always false and I cannot understand why. 我在while语句的开头放置了一个断点,锁变量的值始终为false,我不明白为什么。 The second breakpoint is in the body of the method and I use it in order to block the execution of the first thread and see the the value of the variable locked changed in the second thread, but unfortunately it is not happening. 第二个断点在方法的主体中,我使用它来阻塞第一个线程的执行,并查看第二个线程中锁住的变量的值更改了,但是不幸的是它没有发生。 Therefore, as end results all the threads are able to execute this method without any thread safeness. 因此,最终结果是所有线程都能够执行该方法而没有任何线程安全性。 Does anybody have an idea on how I can provide exclusive access to this method one thread per time? 是否有人对我如何每次一次提供对该线程的独占访问有任何想法?

Only one thread is inside the synchronized block at a time, right? 一次只有一个线程位于同步块中,对吗? You stop your thread at the beginning of the block, and check the value of locked . 您可以在该块的开头停止线程,并检查locked的值。 Well, if your thread is inside the block, that means that no other thread is. 好吧,如果您的线程位于该块内,则意味着没有其他线程。 And if no other thread is inside, then locked has to to be false, that's the whole point. 如果没有其他线程在里面,则locked必须为false,这就是重点。 Why does it surprise you? 为什么会让您感到惊讶?

You don't need the while(locked) wait() loop by the way (or the whole locked thing for that matter) exactly for the reason described above - the whole "locking" is achieved by having the synchronized lock around your code. 您完全不需要由于上述原因while(locked) wait()循环(或者就此而言,整个locked事物)-整个“锁定”是通过对代码进行synchronized锁定来实现的。

The value is always false because only one thread at a time is executing the synchronized block so... when the first thread finished the variable is false again... then, the next thread reads false. 该值始终为false,因为一次仅一个线程正在执行同步块,因此...当第一个线程完成时,变量再次为false ...然后,下一个线程读取为false。

I prefer in this scenarios to use ReentrantLock, something like: 我更喜欢在这种情况下使用ReentrantLock,例如:

protected final ReentrantLock myLock= new ReentrantLock();

...

try {
        //wait until i have the lock
        this.myLock.lock();

        //do some stuff


    } finally {
        //release the lock
        this.myLock.unlock();
    }

And in this article you could see the big difference in performance between synch section and ReentrantLock: http://lycog.com/concurency/performance-reentrantlock-synchronized/ 在本文中,您可以看到synch部分与ReentrantLock之间在性能上的巨大差异: http : //lycog.com/concurency/performance-reentrantlock-synchronized/

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

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