This is the 10th day of my participation in the August More Text Challenge
1. Spin locks for Java locks
Spinlock: the thread that attempts to acquire the lock will not block immediately, but use a loop to acquire the lock. The advantage of this is to reduce the consumption of thread context switch, but the disadvantage is that the loop consumes CPU
Implementation of the spin lock code:
package JUC; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; /** * @Author MingJian_Zhu * @Date 2021/8/13 9:27 */ public class SpinLockDemo { AtomicReference<Thread> atomicReference = new AtomicReference<>(); public void myLock(){ Thread thread = Thread.currentThread(); System.out.println(Thread.currentThread().getName() + "\t come in "); while(! atomicReference.compareAndSet(null,thread)){ } } public void myUnLock(){ Thread thread = Thread.currentThread(); atomicReference.compareAndSet(thread,null); System.out.println(Thread.currentThread().getName() + "\t invoke myUnlock"); } public static void main(String[] args) { SpinLockDemo spinLockDemo = new SpinLockDemo(); new Thread(()->{ spinLockDemo.myLock(); try{ TimeUnit.SECONDS.sleep(5); }catch(InterruptedException e){ e.printStackTrace(); } spinLockDemo.myUnLock(); },"t1").start(); try{ TimeUnit.SECONDS.sleep(1); }catch(InterruptedException e){ e.printStackTrace(); } new Thread(()->{ spinLockDemo.myLock(); spinLockDemo.myUnLock(); },"t2").start(); }}Copy the code
2 Exclusive lock (write lock), shared lock (read lock), and mutex lock
2.1 an exclusive lock
The lock can only be held by one thread at a time. An exclusive lock for both ReentrantLock and Synchronized.
2.2 Shared lock
The lock can be held by multiple thread locks
For ReentrantReadWriteLock, the read lock is shared and the write lock is exclusive
When writing, one person can write, but when reading, more than one person can read simultaneously
2.3 Why are there write locks and read locks
When we use the original ReentranLock create lock, is an exclusive lock, that is a only a thread to access, read and write a separate scenario, however, read to simultaneously, so the original exclusive lock concurrency is not so good, because read lock will not cause data inconsistency problem, so you can read more than one person sharing.
There is no problem with multiple threads reading a resource class at the same time, so it should be possible to read a shared resource at the same time until the amount of concurrency is satisfied, but if a thread wants to write to a shared resource, there should be no other thread that can read or write to the resource
Ps:
-
Read-read: Can coexistCopy the code
-
Read-write: Cannot coexistCopy the code
-
Write - write: Cannot coexistCopy the code
Implementation code:
package JUC; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; /** * @Author MingJian_Zhu * @Date 2021/8/13 11:29 */ public class ReadWriteLockDemo { public static void main(String[] args) { MyCache myCache = new MyCache(); for(int i = 0; i < 5; i++){ final int tempInt = i; new Thread(()->{ myCache.put(tempInt + "",tempInt + ""); },String.valueOf(i)).start(); } for(int i = 0; i < 5; i++){ final int tempInt = i; new Thread(()->{ myCache.get(tempInt + ""); },String.valueOf(i)).start(); } } } class MyCache { private volatile Map<String, Object> map = new HashMap<>(); // private Lock lock = null; /** * define a write operation that satisfies: Atom + exclusive * @param key * @param value */ public void put(String key, Object value) {system.out.println (thread.currentThread ().getName() + "\t: "+ key); Try {/ / simulation network congestion, delay TimeUnit. 0.3 seconds MILLISECONDS. Sleep (300); } catch (InterruptedException e) { e.printStackTrace(); } map.put(key, value); System.out.println(thread.currentThread ().getName() + "\t write done "); } public void get(String key) {system.out.println (thread.currentThread ().getName() + "\t :"); Try {/ / simulation network congestion, delay TimeUnit. 0.3 seconds MILLISECONDS. Sleep (300); } catch (InterruptedException e) { e.printStackTrace(); } Object value = map.get(key); System.out.println(thread.currentThread ().getName() + "\t: "+ value); }}Copy the code
\