1 单例模式
双重校验锁实现对象单例(线程安全):
volatile禁止指令重排序
| public class Singleton {
 private volatile static Singleton uniqueInstance;
 
 private Singleton() {
 }
 
 public  static Singleton getUniqueInstance() {
 
 if (uniqueInstance == null) {
 
 synchronized (Singleton.class) {
 if (uniqueInstance == null) {
 uniqueInstance = new Singleton();
 }
 }
 }
 return uniqueInstance;
 }
 }
 
 | 
uniqueInstance 采用 volatile 关键字修饰也是很有必要的, uniqueInstance = new Singleton(); 这段代码其实是分为三步执行:
- 
为 uniqueInstance分配内存空间
 
- 
初始化 uniqueInstance
 
- 
将 uniqueInstance指向分配的内存地址
 
2 模拟死锁
2.1 线程死锁
| public class DeadLock {private static Object a=new Object();
 private static Object b=new Object();
 
 public static void main(String[] args){
 new Thread(()->{
 synchronized (a){
 System.out.println(Thread.currentThread()+ "获得资源a");
 try {
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 throw new RuntimeException(e);
 }
 System.out.println(Thread.currentThread()+"请求资源b");
 synchronized (b){
 System.out.println(Thread.currentThread()+ "获得资源b");
 }
 }
 },"线程1").start();
 new Thread(()->{
 synchronized (b){
 System.out.println(Thread.currentThread()+ "获得资源b");
 try {
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 throw new RuntimeException(e);
 }
 System.out.println(Thread.currentThread()+"请求资源a");
 synchronized (a){
 System.out.println(Thread.currentThread()+ "获得资源a");
 }
 }
 },"线程2").start();
 }
 }
 
 | 
2.2 数据库事务死锁
| begin;
 
 update user set age = 1 where id = 1;
 
 update user set age = 2 where id = 2;
 commit;
 
 
 
 begin;
 
 update user set age = 3 where id = 2;
 
 update user set age = 4 where id = 1;
 commit;
 
 | 
面试官:请用SQL模拟一个死锁 - 问北 - 博客园 (cnblogs.com)
3 生产者、消费者模型
| class Producer implements Runnable {
 private BlockingQueue<Integer> queue;
 
 public Producer(BlockingQueue<Integer> queue) {
 this.queue = queue;
 }
 
 public void run() {
 try {
 while (true) {
 int value = produce();
 queue.put(value);
 Thread.sleep(1000);
 }
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 
 private int produce() {
 
 return 1;
 }
 }
 
 
 class Consumer implements Runnable {
 private BlockingQueue<Integer> queue;
 
 public Consumer(BlockingQueue<Integer> queue) {
 this.queue = queue;
 }
 
 public void run() {
 try {
 while (true) {
 int value = queue.take();
 consume(value);
 }
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 
 private void consume(int value) {
 
 }
 }
 
 |