package com.boce.gbkutf;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.ibm.icu.util.Calendar;
//To achieve multiple key values and multiple locks, such as key=12, has been locked ( existing in HashMap ), other threads cannot operate on key=12, the thread is blocked, and other threads need to wait for the current thread to complete the operation. can operate.
package com.boce.gbkutf;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.ibm.icu.util.Calendar;
public class MutiLock {
final Lock lock = new ReentrantLock();//Lock object
final Condition notFull = lock.newCondition();//Write thread condition
final Condition notEmpty = lock.newCondition();//Read thread condition
//lock number
final Map<String,String> items = new HashMap<String,String>(100);//Cache queue
// lock a product id
public void put(String x) throws InterruptedException {
lock.tryLock(1000L, TimeUnit.SECONDS); //Lock for 6 seconds
try {
while (x == items.get(x)){//If the object already exists
notFull.await();//Block the writing thread
System.out.println("time:"+Calendar.getInstance().getTimeInMillis());
}
items.put(x, x);//Assignment
System.out.println("put:"+x);
} finally {
lock.unlock();
}
}
//Release a locked item id
public void take(String key) throws InterruptedException {
lock.tryLock(1000L, TimeUnit.SECONDS);
try {
notFull.signal();//Wake up the writing thread
items.remove(key);
System.out.println("================get:"+key);
} finally {
lock.unlock();
}
}
}
// test class
package com.boce.gbkutf;
import java.util.Random;
public class ThreadTest implements Runnable{
private String key;
private MutiLock mutiLock;
public ThreadTest(String key, MutiLock mutiLock) {
super();
this.key = key;
this.mutiLock = mutiLock;
}
@Override
public void run() {
try {
mutiLock.put(key);
Random rand = new Random();
int data = rand.nextInt(2000);
System.out.println("key:"+key+";working hours:"+data);
//simulate working time
Thread.sleep(data);
mutiLock.take(key);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
package com.boce.gbkutf;
public class TestMain {
public static void main(String[] args) {
MutiLock mutiLock = new MutiLock ();
for (int i = 0; i < 2; i++) {
String key = "123456";
ThreadTest test = new ThreadTest(key, mutiLock);
Thread test1 = new Thread(test);
test1.start();
String key1 = "123456"+i;
ThreadTest test11 = new ThreadTest(key1, mutiLock);
Thread test12 = new Thread(test11);
test12.start();
}
}
}
Test log:
put:123456
put:1234561
key: 123456; working hours: 660
put:1234560
key: 1234561; working hours: 795
key: 1234560; working hours: 1090
================get:123456
time:1489373212371
put:123456
key: 123456; working hours: 419
================get:1234561
================get:1234560
================get:123456