厦门建设局网站技227司学校,班级优化大师免费下载电脑版,教人做美食视频网站,关键词提取1 读写锁#xff08;ReadWriteLock#xff09;#x1f4cc; 要点实现类#xff1a;ReentrantReadWirteLock通过读写锁实现更细粒度的控制#xff0c;当然通过Synchronized和Lock锁也能达到目的#xff0c;不过他们会在写入和读取操作都给加锁#xff0c;影响性能#x…1 读写锁ReadWriteLock 要点实现类ReentrantReadWirteLock通过读写锁实现更细粒度的控制当然通过Synchronized和Lock锁也能达到目的不过他们会在写入和读取操作都给加锁影响性能读写锁在加锁同时给读取操作进行优化简单来说性能更高读写锁中读锁是共享锁多个线程可以同时占有写锁是独占锁一次只能被一个线程占有。import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLockDemo {public static void main(String[] args) {MyCache myCache new MyCache();//写入for (int i 0; i 5; i) {final int temp i;new Thread(()-{myCache.put(temp,temp);},String.valueOf(i)).start();}//读取for (int i 0; i 5; i) {final int temp i;new Thread(()-{myCache.get(temp);},String.valueOf(i)).start();}}
}//自定义缓存
class MyCache{private volatile MapString,Object map new HashMap();//读写锁更细粒度的控制private ReadWriteLock readWriteLock new ReentrantReadWriteLock();//存、写入的时候只希望同时只有一个线程写public void put(String key,Object value){readWriteLock.writeLock().lock();try {System.out.println(Thread.currentThread().getName()写入key);map.put(key,value);System.out.println(Thread.currentThread().getName()写入OK);} catch (Exception e) {e.printStackTrace();} finally {readWriteLock.writeLock().unlock();}}//取、读所有人都可以读public void get(String key){readWriteLock.readLock().lock();try {System.out.println(Thread.currentThread().getName()读取key);map.get(key);System.out.println(Thread.currentThread().getName()读取OK);} catch (Exception e) {e.printStackTrace();} finally {readWriteLock.readLock().unlock();}}
}控制台输出2 阻塞队列BlockingQueue阻塞队列BlockingQueue 是一个支持两个附加操作的队列。这两个附加的操作是在队列为空时获取元素的线程会等待队列变为非空。当队列满时存储元素的线程会等待队列可用。 常用实现类 UML相关图 四组API方式抛出异常不会抛异常有返回值阻塞等待超时等待添加操作add()offer()供应put()offer(E e, long timeout,TimeUnit unit)移除操作remove()poll()获得take()poll(long timeout, TimeUnit unit)判断队列首部element()peek()偷看偷窥 代码举例public class BlockingQueueDemo {public static void main(String[] args) throws InterruptedException {//队列的大小ArrayBlockingQueue queue new ArrayBlockingQueue(3);System.out.println(queue.offer(A));System.out.println(queue.offer(B));System.out.println(queue.offer(C));System.out.println(queue.offer(D,2, TimeUnit.SECONDS));System.out.println(------------------------);System.out.println(queue.poll());System.out.println(queue.poll());System.out.println(queue.poll());System.out.println(queue.poll());}控制台输出3 同步队列SynchronousQueue简单来说SynchronousQueue是一个没有数据缓冲的阻塞队列它是实现AbstractQueue接口的。SynchronousQueue容量为0生产者线程插入数据(put)必须等待消费者的移除数据(take)反之亦然也就是说同步队列的插入和移除必须是同步的。public class SynchronousQueueDemo {public static void main(String[] args) throws InterruptedException {SynchronousQueueString queue new SynchronousQueue();//同步队列new Thread(()-{for (int i 1; i 3; i) {try {queue.put(String.valueOf(i));System.out.println(Thread.currentThread().getName()put i);} catch (InterruptedException e) {e.printStackTrace();}}}).start();new Thread(()-{for (int i 1; i 3; i) {try {TimeUnit.SECONDS.sleep(1);System.out.println(Thread.currentThread().getName()take queue.take());} catch (InterruptedException e) {e.printStackTrace();}}}).start();}
}控制台输出 可以看到put和take是伴随的同时执行顺序非固定说明阻塞队列里边其实不存元素。