线程间通信
1、volatile和synchronized关键字
本质上就是“共享内存”式的通信。多个线程需要访问同一个共享变量,谁拿到了锁(获得了访问权限),谁就可以执行。
volatile:保证所有线程对变量访问的可见性
synchronized:保证线程对变量访问的可见性和排他性
2、等待通知机制()
wait方法和notify方法是Object类自带的方法。这个原因是因为任何一个对象都能成为监视器,而wait和notify只有对同一个监视器才能起到预期的作用。也就是说任何一个监视器都能用wait以及notify方法,任何对象都有的方法,自然就需要放到Object中.
wait():wait方法进入了阻塞队列,wait方法执行之后,立刻释放掉锁,这样,另一个线程才能执行同步代码块,才能执行notify。
notify():notify线程会在执行完同步代码之后通知在阻塞队列中的线程,也就是说notify的那个线程并不是立即释放锁,而是在同步方法执行完,释放锁以后,wait方法的那个线程才会继续执行。
在notify方法的线程释放掉锁以后,其通知的线程是不确定的,看具体是哪一个阻塞队列中的线程获取到对象锁。
3、wait和sleep的不同
- wait使线程进入等待,是可以被通知唤醒的,但是sleep只能自己到时间唤醒。
- wait方法是对象锁调用的成员方法,而sleep却是Thread类的静态方法
- wait方法出现在同步方法或者同步代码块中,但是sleep方法可以出现在非同步代码中。
3、管道
(1)PipedInputStream与PipedOutputStream
(2)PipedReader与PipedWriter
public class Piped {
public static void main(String[] args) throws Exception {
PipedWriter out = new PipedWriter();
PipedReader in = new PipedReader();
// 将输出流和输入流进行连接,否则在使用时会抛出IOException
out.connect(in);
Thread printThread = new Thread(new Print(in), "PrintThread");
printThread.start();
int receive = 0;
try {
while ((receive = System.in.read()) != -1) {
out.write(receive);
}
} finally {
out.close();
}
}
static class Print implements Runnable {
private PipedReader in;
public Print(PipedReader in) {
this.in = in;
}
public void run() {
int receive = 0;
try {
while ((receive = in.read()) != -1) {
System.out.print((char) receive);
}
} catch (IOException ex) {
}
}
}
}
结果:输入shu,程序输出shu,
4、Thread.join()
是Thread对象的方法,他的功能是使所属的线程对象x正常执行run方法的内容,而使当前线程z进行无限期的阻塞,等待线程x销毁后在继续执行线程z后面的代码。
public class Join {
public static void main(String[] args) throws Exception {
Thread previous = Thread.currentThread();
for (int i = 0; i < 10; i++) {
// 每个线程拥有前一个线程的引用,需要等待前一个线程终止,才能从等待中返回
Thread thread = new Thread(new Domino(previous), String.valueOf(i));
thread.start();
previous = thread;
}
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName() + " terminate.");
}
static class Domino implements Runnable {
private Thread thread;
public Domino(Thread thread) {
this.thread = thread;
}
public void run() {
try {
thread.join();
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + " terminate.");
}
}
}
5、ThreadLocal线程本地变量
ThreadLocal类提供的几个方法:
public T get() { }
public void set(T value) { }
public void remove() { }
protected T initialValue() { }
get()方法是用来获取ThreadLocal在当前线程中保存的变量副本,set()用来设置当前线程中变量的副本,remove()用来移除当前线程中变量的副本,initialValue()是一个protected方法,一般是用来在使用时进行重写的,它是一个延迟加载方法,下
ThreadLocalMap
ThreadLocalMap的Entry 结构实际上是继承了一个 ThreadLocal 类型的弱引用并将其作为 key,value 为 Object 类型
Entry 源码
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
强引用、软引用、弱引用、虚引用的话,应该可以理解如果使用前两者,对于 GC 的话并不合适,除非强引用置 null 手动通知 GC 回收否则会一直存在在线程生命周期中;而软引用的话,也仅当内存不够时才会回收;虚引用因其特性无法完成 ThreadLocalMap 的所需功能;
使用 WeakReference 类型是出于 GC 考虑,当某个 ThreadLocal 已经没有强引用指向时,它被 GC 回收,那么它的 ThreadLocalMap 里对应的 Entry 的键值会随之失效。
属性
// map 初始容量 16,必须为 2 的幂
private static final int INITIAL_CAPACITY = 16;
// Entry表,大小必须为2的幂
private Entry[] table;
private int size = 0;
//下次需要扩容的阈值,默认 0
private int threshold; // Default to 0
// 2/3 的负载因子
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
构造函数
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
//初始化 table,大小 16
table = new Entry[INITIAL_CAPACITY];
//用第一个健的哈希值对初始大小取模得到索引,和 HashMap 的位运算代替取模原理一样
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
//初始化 Entry
table[i] = new Entry(firstKey, firstValue);
//第一个值进入 table,table 大小置 1
size = 1;
//设置阈值
setThreshold(INITIAL_CAPACITY);
}