实现 LRU
public class LRUCache {
class DLinkedNode {
int key;
int value;
DLinkedNode prev;
DLinkedNode next;
public DLinkedNode() {
}
public DLinkedNode(int _key, int _value) {
key = _key;
value = _value;
}
}
private Map<Integer, DLinkedNode> cache = new HashMap<Integer, DLinkedNode>();
private int size;
private int capacity;
private DLinkedNode head, tail;
public LRUCache(int capacity) {
this.size = 0;
this.capacity = capacity;
head = new DLinkedNode();
tail = new DLinkedNode();
head.next = tail;
tail.prev = head;
}
public int get(int key) {
DLinkedNode node = cache.get(key);
if (node == null) {
return -1;
}
moveToHead(node);
return node.value;
}
public void put(int key, int value) {
DLinkedNode node = cache.get(key);
if (node == null) {
DLinkedNode newNode = new DLinkedNode(key, value);
cache.put(key, newNode);
addToHead(newNode);
++size;
if (size > capacity) {
DLinkedNode tail = removeTail();
cache.remove(tail.key);
--size;
}
} else {
node.value = value;
moveToHead(node);
}
}
private void addToHead(DLinkedNode node) {
node.prev = head;
node.next = head.next;
head.next.prev = node;
head.next = node;
}
private void removeNode(DLinkedNode node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void moveToHead(DLinkedNode node) {
removeNode(node);
addToHead(node);
}
private DLinkedNode removeTail() {
DLinkedNode res = tail.prev;
removeNode(res);
return res;
}
}
实现 单例
Java中实现单例
循环打印 AB
一些知识
- wait() 方法被调用后,线程不会自动苏醒,需要别的线程调用同一个对象上的 notify() 或者 notifyAll() 方法。sleep() 方法执行完成后,线程会自动苏醒。或者可以使用 wait(long timeout) 超时后线程会自动苏醒。
- wait() 方法会释放对应的锁
使用 wait && notify
package com.tattoo.code;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class PrintAAndB01 {
public static Boolean flag = true;
public static int i = 0;
public static Object lock = new Object();
public static void main(String[] args) {
FutureTask<Object> futureTaskA = new FutureTask<>(new PrintA());
FutureTask<Object> futureTaskB = new FutureTask<>(new PrintB());
new Thread(futureTaskA, "线程A").start();
new Thread(futureTaskB, "线程B").start();
}
static class PrintA implements Callable {
@Override
public Object call() throws Exception {
while (i < 10) {
synchronized (lock) {
if (flag) {
lock.wait();
} else {
System.out.println(Thread.currentThread().getName() + "------ A");
flag = !flag;
i++;
lock.notify();
}
}
}
return null;
}
}
static class PrintB implements Callable {
@Override
public Object call() throws Exception {
while (i < 10) {
synchronized (lock) {
if (!flag) {
lock.wait();
} else {
flag = !flag;
System.out.println(Thread.currentThread().getName() + "------ B");
i++;
lock.notify();
}
}
}
return null;
}
}
}
使用 condition
package com.tattoo.code;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class PrintAAndB03 {
private int num = 0;
private ReentrantLock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();
Condition conditionB = lock.newCondition();
private void printABC(int aim, Condition curThread, Condition nextThread) {
for (int i = 0; i < 3; ) {
lock.lock();
try {
while (num % 2 != aim) {
curThread.await();
}
System.out.println(Thread.currentThread().getName());
num++;
i++;
nextThread.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
PrintAAndB03 printABC_lock = new PrintAAndB03();
new Thread(() -> {
printABC_lock.printABC(0, printABC_lock.conditionA, printABC_lock.conditionB);
}, "A").start();
new Thread(() -> {
printABC_lock.printABC(1, printABC_lock.conditionB, printABC_lock.conditionA);
}, "B").start();
}
}