Should i use lock.lock(): in this method?

Posted by user962800 on Stack Overflow See other posts from Stack Overflow or by user962800
Published on 2012-06-09T16:35:26Z Indexed on 2012/06/09 16:40 UTC
Read the original article Hit count: 153

Filed under:

I wrote this method whose purpose is to give notice of the fact that a thread is leaving a

specific block of code

A thread stands for a car which is leaving a bridge so other cars can traverse it .

The bridge is accessible to a given number of cars (limited capacity) and it's one way only.

public void getout(int diection){
        // release the lock
        semaphore.release();

        try{
            lock.lock(); //access to shared data
            if(direction == Car.NORTH)
                nNordTraversing--; //decreasing traversing threads
            else
                nSudTraversing--;

            bridgeCond.signal(); 

        }finally{
            lock.unlock();
        }

    }

My question is: should I use lock.lock(); or it's just nonsense?

thanks in advance

© Stack Overflow or respective owner

Related posts about java