一、前言
本文为自己的学习记录,记录一下自己在面试中遇到的一些手撕代码(非算法题目),也是自己复习方便
二、设计模式
1、工厂模式
(1)简单工厂
// 抽象产品角色
public interface Product {void getInfo();
}
// 真实产品A
public class ProductA implements Product {@Overridepublic void getInfo() {System.out.println("I am productA");}
}
// 真实产品B
public class ProductB implements Product {@Overridepublic void getInfo() {System.out.println("I am productB");}
}
// 工厂类,顺便在这里进行测试
public class Factory {private Product product;public Factory() {}public Product getProductA() {product = new ProductA();return product;}public Product getProductB() {product = new ProductB();return product;}public static void main(String[] args) {Factory factory = new Factory();Product productA = factory.getProductA();Product productB = factory.getProductB();productA.getInfo();productB.getInfo();}
}
(2)抽象工厂
// 还是使用上面的ProductA和ProductB
// 抽象工厂类
public interface Factory {Product getProduct();
}
// 真实工厂A
public class ProductAFactory implements Factory {@Overridepublic Product getProduct() {return new ProductA();}
}
// 真实工厂B
public class ProductBFactory implements Factory {@Overridepublic Product getProduct() {return new ProductB();}
}
// 测试类
public class Main {public synchronized static void main(String[] args) {Factory factoryA = new ProductAFactory();Product productA = factoryA.getProduct();Factory factoryB = new ProductBFactory();Product productB = factoryB.getProduct();productA.getInfo();productB.getInfo();}
}
2、单例模式
(1)双重校验锁
public class Singleton {private static volatile Singleton INSTANCE = null;private Singleton(){};public Singleton getInstance() {if (INSTANCE == null) {synchronized (Singleton.class) {if (INSTANCE == null) {INSTANCE = new Singleton();}return INSTANCE;}}return INSTANCE;}
}
(2)静态内部类
public class Singleton {private Singleton(){};static class SingletonHelper {private static Singleton INSTANCE = new Singleton();}public Singleton getInstance() {return SingletonHelper.INSTANCE;}
}
3、策略模式(待更新)
三、并发
1、读写锁
(1)synchronized实现
class MyReadWriteLock{private volatile int read;private volatile int write;public MyReadWriteLock(){read = 0;write = 0;}public synchronized void readLock() throws InterruptedException {while (write != 0) {wait();}read ++;}public synchronized void unlockRead() {read --;notifyAll();}public synchronized void writeLock() throws InterruptedException {while (write != 0 || read != 0) {wait();}write ++;}public synchronized void unlockWrite() {write --;notifyAll();}}
(2)信号量实现
class ReaderAndWriter{private Semaphore readSemaphore = new Semaphore(1);private Semaphore writerSemaphore = new Semaphore(1);private int count = 0;public void readLock() throws InterruptedException {readSemaphore.acquire();if (count == 0) {writerSemaphore.acquire();}++count;readSemaphore.release();}public void readUnlock() throws InterruptedException {readSemaphore.acquire();--count;if (count == 0) {writerSemaphore.release();}readSemaphore.release();}public void writeLock() throws InterruptedException {writerSemaphore.acquire();}public void writeUnlock() {writerSemaphore.release();}}
2、生产者消费者
1、信号量实现
import java.util.concurrent.Semaphore;public class ProduceAndConsume {private int cacheSize = 0;private Semaphore mutex;private Semaphore full;private Semaphore empty;public ProduceAndConsume(int size) {mutex = new Semaphore(1);full = new Semaphore(0);empty = new Semaphore(size);}public void produce() throws InterruptedException {empty.acquire();mutex.acquire();cacheSize ++;System.out.println("produce..");mutex.release();full.release();}public void consume() throws InterruptedException {full.acquire();mutex.acquire();cacheSize --;System.out.println("consume");mutex.release();empty.release();}
}