# Java多线程
cpu操作系统和线程中间一般会加一级,就是进程(按进程管理资源),抢cpu资源的是线程。 多核时代的cpu架构:共享内存架构(SMP架构)、非一致性内存访问架构(NUMA架构)
# 线程与进程的区别和联系?
一个进程会包含一个或多个线程,进程是操作系统启动程序的运行单元; 线程是操作系统调度运行任务的基本单元; 现代化的操作系统中,两者的概念越来越模糊,进程可以看作是一个重量级一点的线程。
现代化的操作系统中,两者的概念越来越模糊,为什么?
# Thread.start()方法执行后,到底发生了什么?
简述:实际上是调用了操作系统的API来管理了系统线程。
# 创建线程的方式
1.继承Thread 2.实现Runnable接口 3.
# 线程有几种状态?状态是如何流转的?什么情况会到当前状态?
线程有6种状态: New(新建) Runnable(可运行) Blocked(阻塞) Waiting(等待) Timed Waiting(超时等待) Terminated(终止)
# 线程状态流转
# Thread类的方法
# Object类的线程方法
# Thread的中断与异常处理
# 多线程并发问题发生的条件是什么?
# 以下哪些操作是原子的?说明为什么不是原子的。
- x=10;
- y=x;
- x++;
- x=x+1;
# JDK线程池相关的几个类,了解多少?介绍一下?
Executor(执行者): submit方法大部分异常能在主线程中get捕获到,但是类似与除0的算数异常会被吞掉,这是一个小的差异点。
# 对线程池如何实现优雅停机?
# ThreadPoolExecutor的源码看过吗?了解线程池的原理和实现逻辑吗?说说线程池的设计思路?
# 线程池的参数有设置过吗?理解其中的设置逻辑吗?某种场景下,如何设置线程池参数?或者说这样设置合理吗?
# ThreadFactory的作用是什么?
# Java并发容器
# copyOnWriteArrayList
# HashMap:
它的安全问题有哪些?
负载因子的大小带来的影响?
# LinkedHashMap:
基本特征:
实现原理:
与HashMap关系:
# ConcurrentHashMap:
实现原理:
# list
# map
# set
# queue
# 并发容器有哪些坑
# 公平锁和非公平锁的应用场景?
大家一般使用公平锁,线程先来先得,否则在非公平场景下,有些线程可能会饿死。
# 为什么 wait 必须在 synchronized 保护的同步代码中使用?
# 为什么 wait/notify/notifyAll 被定义在 Object 类中,而 sleep 定义在 Thread 类中?
# wait/notify 和 sleep 方法的异同?
# synchronized在同步方法和同步代码块中的不同是?
# 有哪些实现生产者消费者模式的方法?
- 使用BlockingQueue实现生产者消费者模式
public static void main(String[] args) {
BlockingQueue<Object> queue = new ArrayBlockingQueue<>(10);
Runnable producer = () -> {
while (true) {
queue.put(new Object());
}
};
new Thread(producer).start();
new Thread(producer).start();
Runnable consumer = () -> {
while (true) {
queue.take();
}
};
new Thread(consumer).start();
new Thread(consumer).start();
}
- Condition 实现生产者消费者模式
public class MyBlockingQueueForCondition {
private Queue queue;
private int max = 16;
private ReentrantLock lock = new ReentrantLock();
private Condition notEmpty = lock.newCondition();
private Condition notFull = lock.newCondition();
public MyBlockingQueueForCondition(int size) {
this.max = size;
queue = new LinkedList();
}
public void put(Object o) throws InterruptedException {
lock.lock();
try {
while (queue.size() == max) {
notFull.await();
}
queue.add(o);
notEmpty.signalAll();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (queue.size() == 0) {
notEmpty.await();
}
Object item = queue.remove();
notFull.signalAll();
return item;
} finally {
lock.unlock();
}
}
}
- 用 wait/notify 实现生产者消费者模式
class MyBlockingQueue {
private int maxSize;
private LinkedList<Object> storage;
public MyBlockingQueue(int size) {
this.maxSize = size;
storage = new LinkedList<>();
}
public synchronized void put() throws InterruptedException {
while (storage.size() == maxSize) {
wait();
}
storage.add(new Object());
notifyAll();
}
public synchronized void take() throws InterruptedException {
while (storage.size() == 0) {
wait();
}
System.out.println(storage.remove());
notifyAll();
}
}
使用这个 MyBlockingQueue 实现的生产者消费者代码如下:
/**
* 描述:wait形式实现生产者消费者模式
*/
public class WaitStyle {
public static void main(String[] args) {
MyBlockingQueue myBlockingQueue = new MyBlockingQueue(10);
Producer producer = new Producer(myBlockingQueue);
Consumer consumer = new Consumer(myBlockingQueue);
new Thread(producer).start();
new Thread(consumer).start();
}
}
class Producer implements Runnable {
private MyBlockingQueue storage;
public Producer(MyBlockingQueue storage) {
this.storage = storage;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
storage.put();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private MyBlockingQueue storage;
public Consumer(MyBlockingQueue storage) {
this.storage = storage;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
storage.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
← Java API 学习 Java的序列化 →