1、进程、线程
进程:操作系统分配资源的最小单位
线程:操作系统调动的最小单位
2、使用多线程
- 继承
Thread
类
例子:
1 | java复制代码public class Run { |
输出:
thread running
缺点:不支持多继承
2. 实现 Runnable
接口
例子:
1 | java复制代码public class Run { |
输出:
thread running
优点:“间接”支持多继承
3. 分析多线程的命令
* jps+jstack.exe
* jmc.exe(推荐)
* jvisualvm.exe
3、currentThread()
方法
作用:当前代码块正在被哪个线程调用
例子:
1 | java复制代码public class Run { |
输出:
Thread-0
4、isAlive()
方法
作用:当前线程是否处于活动状态(正在运行、准备开始运行)
例子:
1 | java复制代码public class Run { |
输出:
start->false
run->true
end->false
5、sleep(long millis)
方法和sleep(long millis, int nanos)
方法
作用:在指定的时间内(毫秒/毫秒+纳秒)内让“正在执行的线程”休眠,正在执行的线程指 this.currentThread()
返回的线程
例子:
1 | java复制代码public class Run { |
输出:
start time->1637762721620
end time-> 1637762723623
6、getId()
方法
作用:获得线程的唯一标识
例子:
1 | java复制代码public class Run { |
输出:
1
7、停止线程
* `public boolean isInterrupted()`
作用:测试 `currentThread()` 是否已经中断,不清除状态标志
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
java复制代码public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(100);
thread.interrupt();
System.out.println("is thread stop:"+thread.isInterrupted());
System.out.println("is thread stop:"+thread.isInterrupted());
System.out.println("end");
}
}
class MyThread extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 1000; i++) {
System.out.println("i=" + i);
}
}
}
输出:
> i=755
>
> i=756
>
> is thread stop:true
>
> is thread stop:true
>
> i=757
>
> i=758
* `public static boolean interrupted()`
作用:测试 `this` 关键字所在的类是否已经中断,执行后具有清除状态标志为 false 的功能
例子:
1
2
3
4
5
6
7
8
java复制代码public class Run {
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().interrupt();
System.out.println("is thread stop:" + Thread.interrupted());
System.out.println("is thread stop:" + Thread.interrupted());
System.out.println("end");
}
}
输出:
> is thread stop:true
>
> is thread stop:false
>
> end
第二次输出 false 的原因:
`interrupted()` 具有清除状态的功能,第一次调用时,打印了当前线程被中断,然后将状态清除,此时当前线程的状态为不中断;在第二次调用时,由于上次的调用原因,此时的线程状态已经是不中断,所以打印为 false
例子:
1 | java复制代码public class Run { |
输出:
i=686
i=687
already stop,exit
enter exception
3. ######stop()
法暴力停止线程
例子:
1 | java复制代码public class Run { |
输出:
i=374
i=375
i=376
i=377
i=378
缺点:
stop()
会释放锁导致数据不一致,已被弃用
4. return
法停止线程
例子:
1 | java复制代码public class Run { |
输出:
1637766071932
1637766071932
1637766071932
stop
5. 在sleep
状态下停止线程
不管调用顺序,只要 interrupt()
与 stop()
碰到一起就会出现异常
例子:
1 | java复制代码public class Run { |
输出:
thread begin
java.lang.InterruptedException: sleep interrupted
at java.base/java.lang.Thread.sleep(Native Method) at demo.MyThread.run(Run.java:18)
8、暂停线程
suspend()
与resume()
的使用
例子:
1 | java复制代码public class Run { |
输出:
A=1637855269659 i=8483654775
A=1637855274661 i=8483654775
B=1637855279666 i=17266749694
B=1637855284669 i=17266749694
2.suspend()
与resume()
的缺点
* 独占
* 数据不完成
suspend()
与resume()
的替代
suspend()
-> wait()
resume()
-> notify()
/ notifyAll()
9、yield()
方法
作用:放弃当前的 CPU 资源,让其他任务去占用 CPU 时间片
例子:
1 | java复制代码public class Run { |
输出:
time:1
time:24
10、线程的优先级
设置优先级 public final void setPriority(int newPriority)
预定三个优先级:
1 | java复制代码 /** |
优先级具有继承性,例如A线程启动B线程,则B的优先级和A一样
11、守护线程
Java中存在两种线程:用户线程(非守护线程)、守护线程
只有进程中不存在非守护线程,则守护线程自动销毁;若存在任何一个非守护线程,守护线程就要继续工作
主线程(main)属于非守护线程
设置守护线程:setDaemon(true)
,需要在 start()
方法前设置
例子:
1 | java复制代码public class Run { |
输出:
i=1
i=2
i=3
i=4
i=5
exit
本文转载自: 掘金