本文主要是自己学习多线程时,所做的笔记,参考的是b站狂神说java多线程视频讲解。
线程方法
方法 | 说明 |
---|---|
setPriority(int newPriority) | 更改线程的优先级 |
static void sleep(long millis) | 在指定的好描述内让当前正在执行的线程休眠 |
void join() | 等待该线程终止 |
static void yield() | 暂停当前正在执行的线程,并执行其他线程 |
void interrupt() | 中断线程,不要用这个方法 |
boolean isAlive() | 测试线程是否处于活动状态 |
1.停止线程 (标志位)
不推荐使用JDK提供的stop(),destroy()方法。
建议使用一个标志位进行终止变量,当flag=false,则终止线程运行。
具体代码如下:
/**
* @author zyy
* @create 2021-10-29 16:20
*/
public class TestStop implements Runnable{
//1.线程中定义线程体使用的标识
private boolean flag=true;
@Override
public void run() {
int i=0;
//2.线程体使用该标识
while(flag){
System.out.println("Thread ----- run"+i++);
}
}
//3.设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
TestStop testStop=new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 100; i++) {
System.out.println("main"+i);
if(i==90){
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程该停止了"+i);
}
}
}
}
2.线程休眠 sleep()
sleep(时间) 指定当前线程阻塞的毫秒数;
sleep存在异常InterruptedException;
sleep时间到达后线程进入就绪状态;
sleep可以模拟网络延时,倒计时等;
每一个对象都有一个锁,sleep不会释放锁;
以倒计时为例
/**
* @author zyy
* @create 2021-10-29 16:44
*
*测试sleep()方法,模拟倒计时
*/
public class TestSleep {
public static void main(String[] args) {
try {
tenDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void tenDown() throws InterruptedException {
int num=10;
while(true){
Thread.sleep(1000);
System.out.println(num--);
if(num<=0){
break;
}
}
}
}
3.线程礼让 yield
礼让线程,让当前正在执行的线程暂停,但不阻塞。
比如就绪礼让线程A和线程B同时争夺cpu时,当A拿到了cpu资源,执行礼让yield(),则放弃cpu资源,与线程B重新竞争。因此礼让不一定会成功。
/**
* @author zyy
* @create 2021-10-29 16:51
*
* 测试礼让线程
* 礼让不一定成功,看cpu心情
*
* 下面例子让线程a进行礼让
*/
public class TestYield {
public static void main(String[] args) {
MyYield myYield=new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
if(Thread.currentThread().getName().equals("a")){
Thread.yield();
}
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
礼让成功 礼让不成功
4.线程强制执行 join
Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞;
/**
* @author zyy
* @create 2021-10-29 17:09
*
* 测试线程强制执行 join
*
* 在本案例中有创建的thread线程和main主线程,最开始thread和main线程并发执行,
* 当主线程执行到i=4时,thread通过join方法,强制执行,待该线程执行完成后,
* 再执行其他线程,其他线程阻塞
*
*/
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin=new TestJoin();
Thread thread =new Thread(testJoin);
thread.start();
//主线程
for(int i=0;i<10;i++){
if(i==4){
thread.join(); //插队
}
System.out.println("main"+i);
}
}
}
5.观测线程状态
/**
* @author zyy
* @create 2021-10-29 18:17
*/
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("");
}
});
//观察状态
Thread.State state =thread.getState();
System.out.println(state);
//观察启动后
thread.start();
state=thread.getState();
System.out.println(state);
while(state !=Thread.State.TERMINATED){ //只要线程不终止,就一直输出状态
Thread.sleep(100);
state=thread.getState();//更新线程状态
System.out.println(state); //输出状态
}
}
}
6.线程优先级
线程优先级用数字表示,范围1~10
Thread.MIN_PRIORITY=1;
Thread.MAX_PRIORITY=10;
Thread.NORM_PRIORITY=5;
使用一下方式改变或获取优先级
getPriority(); setPriority(int xxx);
/**
* @author zyy
* @create 2021-10-29 18:28
*
* 测试线程的优先级
*
* 优先级越高一般会先被cpu调度,但并不是就一定会先调度
*/
public class TestPriority {
public static void main(String[] args) {
//主线程不能设置优先级
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority myPriority=new MyPriority();
Thread t1=new Thread(myPriority);
Thread t2=new Thread(myPriority);
Thread t3=new Thread(myPriority);
//先设置优先级,再启动
t1.setPriority(1);
t1.start();
t2.setPriority(5);
t2.start();
t3.setPriority(Thread.MAX_PRIORITY);
t3.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
};
7.守护线程
线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕,如后台几乎操作日志,监控内存,垃圾回收等
/**
* @author zyy
* @create 2021-10-29 19:12
*/
public class TestDaemon {
public static void main(String[] args) {
God god=new God();
You you=new You();
Thread thread=new Thread(god);
thread.setDaemon(true); //默认为false表示用户线程,正常的线程都是用户线程
thread.start(); //守护线程启动
new Thread(you).start(); //用户线程启动
}
}
class God implements Runnable{
public void run(){
while (true){
System.out.println("我是守护线程");
}
}
}
class You implements Runnable{
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("我是用户线程");
}
System.out.println("========= goodbye! ==========");
}
}