1.线程运行原理
栈与栈帧
Java Virtual Machine Stacks (Java 虚拟机栈)
我们都知道 JVM 中由堆、栈、方法区所组成,其中栈内存是给谁用的呢?栈内存是给线程用
,每个线程启动后,虚拟机就会为其分配一块栈内存。
- 每个栈由多个栈帧(Frame)组成,对应着每次方法调用时所占用的内存
- 每个线程只能有一个活动栈帧,对应着当前正在执行的那个方法
以下图是通过debug的方式,显示栈内存的变化
public class TestFrames {
public static void main(String[] args) {
method1(10);
}
private static void method1(int x) {
int y = x + 1;
Object m = method2();
System.out.println(m);
}
private static Object method2() {
Object n = new Object();
return n;
}
}
java.lang.Object@d716361
public class TestFrames {
public static void main(String[] args) {
Thread t1 = new Thread(){
@Override
public void run() {
method1(20);
}
};
t1.setName("t1");
t1.start();
method1(10);
}
private static void method1(int x) {
int y = x + 1;
Object m = method2();
System.out.println(m);
}
private static Object method2() {
Object n = new Object();
return n;
}
}
java.lang.Object@d716361
java.lang.Object@4d13cc6d
线程上下文切换
因为以下一些原因导致 cpu 不再执行当前的线程,转而执行另一个线程的代码
- 线程的 cpu 时间片用完
- 垃圾回收
- 有更高优先级的线程需要运行
- 线程自己调用了 sleep、yield、wait、join、park、synchronized、lock 等方法
当 Context Switch 发生时,需要由操作系统保存当前线程的状态,并恢复另一个线程的状态,Java 中对应的概念就是程序计数器(Program Counter Register),它的作用是记住下一条 jvm 指令的执行地址,是线程私有的 - 状态包括程序计数器、虚拟机栈中每个栈帧的信息,如局部变量、操作数栈、返回地址等
- Context Switch 频繁发生会影响性能
2.线程常见方法
2.1 start与run
总结:
1.直接调用 run 是在主线程中执行了 run,没有启动新的线程
2.使用 start 是启动新的线程,通过新的线程间接执行 run 中的代码
start
Test5 [t1]
- running…
public class Test5 {
public static void main(String[] args) {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("running...");
}
};
t1.start();//启动的是t1线程
}
}
20:53:37.914 c.Test5 [t1] - running...
run
如果只是使用,run(),是主线程
20:54:43.445 c.Test5[main]
- running…
public class Test5 {
public static void main(String[] args) {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("running...");
}
};
t1.run();
}
}
20:54:43.445 c.Test5 [main] - running...
2.2 sleep,interrupt,TimeUnit,yield
sleep
- 调用
sleep
会让当前线程从 Running 进入 Timed Waiting 状态(阻塞) - 其它线程可以使用
interrupt
方法打断正在睡眠的线程,这时 sleep 方法会抛出InterruptedException
- 睡眠结束后的线程未必会立刻得到执行
- 建议用
TimeUnit
的 sleep 代替Thread
的 sleep 来获得更好的可读性
1) 没有使用sleep()
public class Test {
public static void main(String[] args) {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("running...");
}
};
System.out.println(t1.getState());
t1.start();
System.out.println(t1.getState());
}
}
NEW
RUNNABLE
20:59:54.931 c.Test [t1] - running...
2)使用使用sleep()
sleep(long n)
让当前执行的线程休眠n毫秒,休眠时让出 cpu的时间片给其它线程
public class Test {
public static void main(String[] args) {
Thread t1 = new Thread("t1") {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
log.debug("t1 state: {}", t1.getState());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("t1 state: {}", t1.getState());
}
}
21:02:33.511 c.Test [main] - t1 state: RUNNABLE
21:02:34.024 c.Test [main] - t1 state: TIMED_WAITING
3)使用interrupt()方法
interrupt() 打断线程
如果被打断线程正在 sleep,wait,join 会导致被打断的线程抛出 InterruptedException,并清除打断标记;如果打断的正在运行的线程,则会设置 打断标记;park 的线程被打断,也会设置打断标记
public class Test7 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("enter sleep...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
log.debug("wake up...");
e.printStackTrace();
}
}
};
t1.start();
Thread.sleep(1000);
log.debug("interrupt...");
t1.interrupt();
}
}
21:05:36.301 c.Test7 [t1] - enter sleep...
21:05:37.305 c.Test7 [main] - interrupt...
21:05:37.305 c.Test7 [t1] - wake up...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at cn.itcast.test.Test7$1.run(Test7.java:14)
4)TimeUnit()
public class Test8 {
public static void main(String[] args) throws InterruptedException {
log.debug("enter");
TimeUnit.SECONDS.sleep(1);
log.debug("end");
// Thread.sleep(1000);
}
}
21:08:34.110 c.Test8 [main] - enter
21:08:35.123 c.Test8 [main] - end
yield
提示线程调度器让出当前线程对CPU的使用,主要是为了测试和调试
- 调用
yield
会让当前线程从 Running 进入 Runnable 就绪状态,然后调度执行其它线程 - 具体的实现依赖于操作系统的任务调度器
线程优先级
- 线程优先级会提示(hint)调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它
- 如果 cpu 比较忙,那么优先级高的线程会获得更多的时间片,但 cpu 闲时,优先级几乎没作用
2.3 join方法
1)未使用join方法
public class Test10 {
static int r = 0;
public static void main(String[] args) throws InterruptedException {
test1();
}
private static void test1() throws InterruptedException {
log.debug("开始");
Thread t1 = new Thread(() -> {
log.debug("开始");
sleep(1);
log.debug("结束");
r = 10;
},"t1");
t1.start();
log.debug("结果为:{}", r);
log.debug("结束");
}
}
21:11:48.785 c.Test10 [main] - 开始
21:11:48.806 c.Test10 [t1] - 开始
21:11:48.806 c.Test10 [main] - 结果为:0
21:11:48.807 c.Test10 [main] - 结束
21:11:49.820 c.Test10 [t1] - 结束
2)未使用join方法
public class Test10 {
static int r = 0;
public static void main(String[] args) throws InterruptedException {
test1();
}
private static void test1() throws InterruptedException {
log.debug("开始");
Thread t1 = new Thread(() -> {
log.debug("开始");
sleep(1);
log.debug("结束");
r = 10;
},"t1");
t1.start();
t1.join();
log.debug("结果为:{}", r);
log.debug("结束");
}
}
21:13:21.861 c.Test10 [main] - 开始
21:13:21.883 c.Test10 [t1] - 开始
21:13:22.890 c.Test10 [t1] - 结束
21:13:22.890 c.Test10 [main] - 结果为:10
21:13:22.892 c.Test10 [main] - 结束
总结:
因为主线程和线程 t1 是并行执行的,t1 线程需要 1 秒之后才能算出 r=10,而主线程一开始就要打印 r 的结果,所以只能打印出 r=0
解决方法
用 join,加在 t1.start() 之后即可
3) 同步
- 需要等待结果返回,才能继续运行就是同步
- 不需要等待结果返回,就能继续运行就是异步
public class Test10 {
static int r1 = 0;
static int r2 = 0;
public static void main(String[] args) throws InterruptedException {
test2();
}
private static void test2() throws InterruptedException {
Thread t1 = new Thread(() -> {
sleep(1);
r1 = 10;
});
Thread t2 = new Thread(() -> {
sleep(2);
r2 = 20;
});
long start = System.currentTimeMillis();
t1.start();
t2.start();
t1.join();
t2.join();
long end = System.currentTimeMillis();
log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);
}
}
21:35:09.410 c.Test10 [main] - r1: 10 r2: 20 cost: 2008
t1和t2的join()交换位置后,代码如下
public class Test10 {
static int r1 = 0;
static int r2 = 0;
public static void main(String[] args) throws InterruptedException {
test2();
}
private static void test2() throws InterruptedException {
Thread t1 = new Thread(() -> {
sleep(1);
r1 = 10;
});
Thread t2 = new Thread(() -> {
sleep(2);
r2 = 20;
});
long start = System.currentTimeMillis();
t1.start();
t2.start();
t2.join();//t2和t1交换了位置
t1.join();
long end = System.currentTimeMillis();
log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);
}
}
21:30:57.485 c.Test10 [main] - r1: 10 r2: 20 cost: 2003
分析如下:
- 第一个 join:等待 t1 时, t2 并没有停止, 而在运行
- 第二个 join:1s 后, 执行到此, t2 也运行了 1s, 因此也只需再等待 1s