java ReentrantLock application

java ReentrantLock application

1. Using the ReentrantLock interface is more efficient than using the synchronized


keyword . The difference between unfair locks and fair locks:
private ReentrantLock lock = new ReentrantLock(); //The parameter defaults to false, unfair lock
private ReentrantLock lock = new ReentrantLock(true); //公平锁

1. Under fair conditions, operations will be executed in a queue to ensure the order of execution. (It will consume more time to queue)
2. In the case of unfairness, it is the disordered state that allows the queue to be cut, and the jvm will automatically calculate how to deal with it faster to schedule the queue cut. (This is faster if you don't care about the order)


Example:
import java.util.concurrent.locks.ReentrantLock;

public class addI extends Thread {
	private static int i = 0;
	private static ReentrantLock reentrantLock = new ReentrantLock();
	private int threadId = 0;

	@Override
	public void run() {
		//wait one by one
        try {
        	reentrantLock.lock();
        	i++;
        } catch (Exception e) {
            e.printStackTrace ();
        } finally {
            // unlock
        	reentrantLock.unlock();
        }

      //If it has been locked, it will not be locked again
// if (reentrantLock.tryLock()) { //If you can't get the lock, just don't execute it
//        	 try {
//             	i++;
//             } catch (Exception e) {
// e.printStackTrace ();
//             } finally {
// // unlock
//             	reentrantLock.unlock();
//             }
//        }

      //If it is found that the operation is already executing, try to wait for a period of time, and wait for the timeout to not execute (try to wait for execution) (this method will be used more in practical applications)
//        try {
// //If it has been locked, try to wait 5s to see if the lock can be obtained. If the lock cannot be obtained after 5s, return false to continue execution
//        	if (reentrantLock.tryLock(5, TimeUnit.SECONDS)) {
//        		 try {
//        			 i++;
//        		 }catch (Exception e) {
// e.printStackTrace ();
//        	        } finally {
// // unlock
//        	        	reentrantLock.unlock();
//        	        }
//        	 }
//
//        } catch (InterruptedException e) {
// e.printStackTrace(); //When the current thread is interrupted (interrupt), InterruptedException will be thrown
//        }

        // interruptible lock
//        try {
// reentrantLock.lockInterruptibly(); //Can be interrupted with the thread's .interrupt() interface
// //operate
//        } catch (InterruptedException e) {
// e.printStackTrace ();
//        } finally {
//        	reentrantLock.unlock();
//        }

		System.out.println("threadId = " + threadId + " i= " + i);
	}

	public addI(int threadId) {
		super();
		this.threadId = threadId;
	}
}


public class ReentrantLockTest {
	public static void main(String[] args) {
		Thread thread[] = new Thread[10];
		for (int i = 0; i < 10; i++) {
			thread[i] = new addI(i);
		}
		for (int i = 0; i < 10; i++) {
			thread[i].start();
		}
	}
}




Reference text: http://blog.csdn.net/woaieillen/article/details/8046876
Reference text: http://www.cnblogs.com/xiaorenwu702/p/3975916.html
Reference text: https://my.oschina .net/andylucci/blog/651982

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038334&siteId=291194637