For multiple lock attempts, use Java concurrent. ReentrantLock

I noticed the following code block:

final Lock s = new reentrantlock();
    for(int i = 0 ; i < 1000 ; i++)
    {
        s.lock();
        System.out.println(i+" :" +s.tryLock()+" ");
    }

Printed matter:

0 :true 
1 :true 
2 :true 
3 :true 
...

It's strange - I want successive locks to fail because s will never be unlocked

What's wrong here?

Solution

Javadoc is your friend. You really should read it

From: reentrantlock lock()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>