锁定两个方法但允许一个方法运行多线程

时间:2014-08-26 10:49:49

标签: java c# multithreading thread-safety locking

我有两种方法:

Foo(){...} Bar(){...}

我想创建一个锁定:

  1. 虽然在Foo中运行了一个线程,但是在Foo方法中没有线程之前,线程将被锁定在Bar上并且无法运行。

  2. 在Foo中可以同时运行多个线程。

  3. 简单来说,锁定:"不允许同时运行Foo和Bar,但允许Foo或Bar运行多个线程"。

    注意:简单锁定不会起作用,因为它会维持条件1,但它不会支持条件2.

1 个答案:

答案 0 :(得分:2)

你只需要确保没有人正在执行另一种方法,你可以通过计算每种方法的线程数来实现这一点。 我没有使用锁,只是等待和同步。

int numFoo = 0;
int numBar = 0;

void foo(){
    enter(0);
    //stuff
    leave(0);
}

void bar(){
    enter(1);
    //Stuff
    leave(1);
}

//Synchronized makes no more than one thread can run this simultaneously, so they wont be any race condition
synchronized void enter(int id){
    //If it was foo entering
    if (id == 0){
        while(numBar != 0)//Waits until theres none executing bar
            try {
                this.wait();
            } catch (InterruptedException e) {e.printStackTrace();}
        numFoo++;//Enters
    }
    else{
        while(numFoo !=0)//Waits until none executes foo
            try { 
                this.wait();
            } catch (InterruptedException e) { e.printStackTrace();}
        numBar++;//Enters
    }
}

synchronized void leave(int id){
    if (id == 0){
        numFoo--;
        if(numFoo == 0)//If it was the last executing foo unlocks the others
            this.notifyAll();
        }
    else{
        numBar--;
        if(numBar == 0)//If it was the last executing bar unlocks the others
            this.notifyAll();
    }
}

“while(numbar!= 0)this.wait()”有必要像 notifyAll()一样工作。 没有它,可能会发生另一个线程在我们解锁并尝试输入foo的线程到达foo ++之前开始执行bar,导致同时执行 bar和foo。

相关问题