package com.zzh.test;
public class Thread {
static java.lang.Thread a = new java.lang.Thread(new Runnable() {
public void run() {
System.out.println("线程A");
}
});
static java.lang.Thread b = new java.lang.Thread(new Runnable() {
public void run() {
System.out.println("线程b");
}
});
static java.lang.Thread c = new java.lang.Thread(new Runnable() {
public void run() {
System.out.println("线程c");
}
});
public static void main(String[] args) throws InterruptedException {
a.start();
a.join();
b.start();
b.join();
c.start();
}
}
通过join。
join :让主线程等待子线程结束以后才能继续运行;
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
队列的方式FIFO
volatile
1.对于申明了volatile的变量进行写操作的时候,JVM会向处理器发送一条lock前缀的指令,会把这个变量所在的缓存行写回到系统内存中.
2.在多处理器的情况下,保证各个处理器缓存一致性的特点。
synchronized
可以做重入锁,互斥性,可见性。
volatile可以做到原子性,可见性。不能做到复合操作的原子性。