![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
多线程面试
明天请叫我早点起床
这个作者很懒,什么都没留下…
展开
-
Callable
Callablepublic class Test { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<Integer> futureTask = new FutureTask(new MyCallable()...原创 2019-07-18 08:46:10 · 100 阅读 · 0 评论 -
SynchronousQueue
SynchronousQueue每一个put操作必须要等待一个take操作,否则不会继续添加元素。public class Test { public static void main(String[] args) { SynchronousQueue<String> synchronousQueue = new SynchronousQueue();...原创 2019-07-16 20:56:33 · 163 阅读 · 0 评论 -
死锁编码及定位分析
死锁编码及定位分析class Resource{ String re_a = "re_a"; String re_b = "re_b";}public class Test { public static void main(String[] args) { Resource s = new Resource(); new Thr...原创 2019-07-18 19:27:20 · 121 阅读 · 0 评论 -
信号量模拟6辆车抢三个车位的情况
信号量模拟6辆车抢三个车位的情况public class Test { public static void main(String[] args) { Semaphore semaphore = new Semaphore(3);//模拟3个停车位 for (int i = 1; i <= 6; i++) { //模拟6辆车 ...原创 2019-07-16 15:20:33 · 356 阅读 · 0 评论 -
CountDownLatch
CountDownLatchpublic class Test { public static void main(String[] args) throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(5); for (int i = 1; i <...原创 2019-07-16 13:49:36 · 93 阅读 · 0 评论 -
读写锁 ReetrantReadWriteLock
读写锁 ReetrantReadWriteLockclass MyHashMap{ private volatile Map<Object,Object> map = new HashMap<>(); private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); pub...原创 2019-07-16 13:08:01 · 143 阅读 · 0 评论 -
手写一个自旋锁
手写一个自旋锁class MySpinLock{ AtomicReference<Thread> reference = new AtomicReference<>(); public void lock(){ System.out.println(Thread.currentThread().getName()+" want to ge...原创 2019-07-16 11:38:47 · 483 阅读 · 0 评论 -
自定义一个线程池
自定义一个线程池public class Test2 { public static void main(String[] args) { ExecutorService service = new ThreadPoolExecutor( 2, //...原创 2019-07-18 14:19:06 · 139 阅读 · 0 评论 -
Executors的常用三种线程池
常用三种线程池 newFixedThreadPool定义一个定长线程池,可控制最大线程并发数,超过的线程会在队列中等待 public class Test { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPo...原创 2019-07-18 14:12:35 · 908 阅读 · 0 评论 -
生产者-消费者
生产者-消费者 传统版本/** * 生产者,消费者传统版 */class MyData{ //资源类 private int number; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void inc...原创 2019-07-17 14:35:06 · 95 阅读 · 0 评论