解锁是如何在等待之后发生的

huangapple go评论79阅读模式
英文:

How is unlock happening after await

问题

我写了一个小程序来交替打印奇偶数,但有一个问题:

由于线程在await调用处应该等待,那么可重入锁是如何被解锁的?

public class Worker implements Runnable
{
    private ReentrantLock rLock = null;
    private Condition condition = null;
    private String name;
    volatile static boolean isEvenTurn = true;
    
    public Worker(String name, ReentrantLock rLock, Condition condition)
    {
        this.name = name;
        this.rLock = rLock;
        this.condition = condition;
    }
    
    @Override
    public void run() 
    {
        try
        {
            if(name.equals("ODD"))
                printOdd();
            else
                printEven();
        }
        catch(Exception e) { e.printStackTrace();}
        
    }
    
    private void printOdd() throws Exception
    {
        while(isEvenTurn);
        for(int i=1;i<10;i+=2)
        {
            try
            {
                rLock.lock();
                System.out.println(i);
            }
            catch(Exception e) {e.printStackTrace();}
            finally
            {
                condition.signal();
                condition.await();
                rLock.unlock();
            }
        }
    }
    
    private void printEven() throws Exception
    {
        for(int i=0;i<10;i+=2)
        {
            try
            {
                rLock.lock();
                System.out.println(i);
                isEvenTurn = false;
            }
            catch(Exception e) {e.printStackTrace();}
            finally
            {
                condition.signal();
                condition.await();
                rLock.unlock();
            }
        }
    }
    
    public static void main(String[] args) 
    {
        ReentrantLock rLock = new ReentrantLock();
        ExecutorService service = Executors.newFixedThreadPool(2);
        
        Condition c = rLock.newCondition();
        Worker oddPrinter = new Worker("ODD",rLock,c);
        Worker evenPrinter = new Worker("EVEN",rLock,c);
        
        service.execute(evenPrinter);
        service.execute(oddPrinter);
        
        service.shutdown();
    }
}
英文:

I wrote a small program to print odd-even numbers alternatively but have a question:

Since thread should wait at await call so how is reentrant lock is getting unlocked?

public class Worker implements Runnable
{
private ReentrantLock rLock = null;
private Condition condition = null;
private String name;
volatile static boolean isEvenTurn = true;
public Worker(String name, ReentrantLock rLock, Condition condition)
{
this.name = name;
this.rLock = rLock;
this.condition = condition;
}
@Override
public void run() 
{
try
{
if(name.equals(&quot;ODD&quot;))
printOdd();
else
printEven();
}
catch(Exception e) { e.printStackTrace();}
}
private void printOdd() throws Exception
{
while(isEvenTurn);
for(int i=1;i&lt;10;i+=2)
{
try
{
rLock.lock();
System.out.println(i);
}
catch(Exception e) {e.printStackTrace();}
finally
{
condition.signal();
condition.await();
rLock.unlock();
}
}
}
private void printEven() throws Exception
{
for(int i=0;i&lt;10;i+=2)
{
try
{
rLock.lock();
System.out.println(i);
isEvenTurn = false;
}
catch(Exception e) {e.printStackTrace();}
finally
{
condition.signal();
condition.await();
rLock.unlock();
}
}
}
public static void main(String[] args) 
{
ReentrantLock rLock = new ReentrantLock();
ExecutorService service = Executors.newFixedThreadPool(2);
Condition c = rLock.newCondition();
Worker oddPrinter = new Worker(&quot;ODD&quot;,rLock,c);
Worker evenPrinter = new Worker(&quot;EVEN&quot;,rLock,c);
service.execute(evenPrinter);
service.execute(oddPrinter);
service.shutdown();
}
}

答案1

得分: 2

在printEven()方法中添加以下代码:在finally块中:

finally
{
condition.signal();
if (i < 10) condition.await(); 
rLock.unlock();
}
通过添加这个条件,当i = 10时,你的线程将不再等待。
英文:

In printEven() method add this line: in finally block:

       finally
{
condition.signal();
if(i &lt; 10)condition.await(); 
rLock.unlock();
}

By adding this condition, when your
i = 10 your thread will not wait anymore.

huangapple
  • 本文由 发表于 2020年9月3日 12:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63716866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定