- 使用多线程
- extends Thread
- new MyThread()
- implements Runnable
- new Thread(new MyRunnable)
- 共享变量使用synchronized 进行加锁
- extends Thread
- api汇总
- currentThread()
- 当前的线程
- isAlive()
- 判断线程是否存活
- sleep
- 线程休眠
- gitId()
- 获取当前线程id
- interrupt()
- 终止线程,但不会停止线程。打一个log?
- interruptted()
- static 方法,判断是否终止。调用后清除状态
- isInterruptted()
- 判断是否被终止,非statis类型方法。
- throw new InterruptException()
- 判断是否终止并使用抛出异常来暂停程序
- stop()
- 暴力停止,数据得不到清理,数据不一致
- return()
- 判断是否被暂停,使用return 结束,会污染代码
- suspend()
- 线程暂停
- resume()
- 线程恢复
线程暂停恢复可能会导致数据不一致,对象独占情况,被弃用
- 线程恢复
- yield()
- 释放当前CPU资源
- priority()(1-10)
- 并不一定先执行完优先级高的再执行优先级低的
- 优先级高的比优先级低的执行速度块
- daemon()
- 守护线程
- currentThread()