In the java.util.concurrent.locks package, the ReentrantLock class implements the lock interface, which is used to lock and unlock restrictions. After the lock is unlocked, other threads can enter and execute, otherwise deadlock will occur.
lockInterruptibly() Method Introduction:
This method returns the method to get the lock, but when the thread calls the interrupt() method, the method returns an exception that causes the thread to interrupt. Thread interruption.
The code example is as follows:
package TestThread.ThreadLockDemo; import java.util.concurrent.locks.ReentrantLock; public class TestLock { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock();// Initialize lock object Test2 test2 = new Test2("Apple", 100);// Initialize the number of apples Test1 test1 = new Test1(lock, 10, test2);// Initialize Thread objects Thread t1 = new Thread(test1, "Thread 1");// Create thread Thread t2 = new Thread(test1, "Thread 2"); Thread t3 = new Thread(test1, "Thread 3"); Thread t4 = new Thread(test1, "Thread 4"); Thread t5 = new Thread(test1, "Thread 5"); // t1.start();// Start Threads t2.start(); t4.start(); t5.start(); t4.interrupt(); t3.start(); } } class Test1 implements Runnable { private int count; private ReentrantLock lock; private Test2 test2; public Test1(ReentrantLock lock, int count, Test2 test2) { this.lock = lock; this.count = count; this.test2 = test2; } @Override public void run() { // try { // lock.lock(); // Thread.sleep(1); // System. out. println (Thread. current Thread (). getName ()+": successfully unlocked"); // } catch (InterruptedException e) { // e.printStackTrace(); // } finally { // lock.unlock(); // System. out. println (Thread. current Thread (). getName ()+": successful latch-up"); // } // lock.tryLock() test // if (lock.tryLock()) { // try { // // Thread.sleep(1000); // System. out. println (Thread. current Thread (). getName ()+": The current thread has been unlocked!" // } catch (Exception e) { // System. out. println (Thread. current Thread (). getName ()+": The current thread is interrupted!" // } finally { // lock.unlock(); // System. out. println (Thread. current Thread (). getName ()+": The lock was released successfully!" // } // } else { // System. out. println (Thread. current Thread (). getName ()+": No lock was acquired!" // } try { lock.lockInterruptibly(); try { System.out.println(Thread.currentThread().getName() + ":Current thread acquires thread locks!"); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + ":System exception occurred in the current thread!"); } finally { lock.unlock(); System.out.println(Thread.currentThread().getName() + ":Release the lock successfully!"); } } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + ":Current thread interrupt!"); } } } class Test2 { private String name; int count; /** * @param name The Name of Apple * @param count Initialize the number of apples */ public Test2(String name, int count) { this.name = name; this.count = count; } /** * * @author Author E-mail: * * @date Creation time: March 24, 2017, 1:13:14 p.m. * @version 1.0 * @parameter * @since * @return */ public void DiscountApple(int discount) { this.count = this.count - discount; System.out.println(Thread.currentThread().getName() + ":The number of apples is:" + this.count + ",Sold out" + discount); } }
The results are as follows:
The update code is as follows:
package TestThread.ThreadLockDemo; import java.util.concurrent.locks.ReentrantLock; public class TestLock { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock();// Initialization lock object Test2 test2 = new Test2("Apple", 100);// Initialize the number of apples Test1 test1 = new Test1(lock, 10, test2);// Initialize Thread objects Thread t1 = new Thread(test1, "Thread 1");// Create thread Thread t2 = new Thread(test1, "Thread 2"); Thread t3 = new Thread(test1, "Thread 3"); Thread t4 = new Thread(test1, "Thread 4"); Thread t5 = new Thread(test1, "Thread 5"); Thread t6 = new Thread(test1, "Thread 6"); // t1.start();// Startup thread t2.start(); t4.start(); t5.start(); t4.interrupt(); t3.start(); t6.start(); } } class Test1 implements Runnable { private int count; private ReentrantLock lock; private Test2 test2; public Test1(ReentrantLock lock, int count, Test2 test2) { this.lock = lock; this.count = count; this.test2 = test2; } @Override public void run() { try { lock.lockInterruptibly(); try { Thread.sleep(1000); test2.DiscountApple(count); // Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + ":System exception occurred in the current thread!"); } finally { lock.unlock(); System.out.println(Thread.currentThread().getName() + ":Release the lock successfully!"); } } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + ":Current thread interrupt!"); } } } class Test2 { private String name; int count; /** * @param name The Name of Apple * @param count Initialize the number of apples */ public Test2(String name, int count) { this.name = name; this.count = count; } /** * * @author Author E-mail: * * @date Creation time: March 24, 2017, 1:13:14 p.m. * @version 1.0 * @parameter * @since * @return */ public void DiscountApple(int discount) { this.count = this.count - discount; System.out.println(Thread.currentThread().getName() + ":The number of apples is:" + this.count + ",Sold out" + discount); } }
The results of implementation are as follows: