线程状态

1.线程方法
| 方法 | 说明 |
| setPriority(int newPriority) | 更改线程的优先级 |
| static void sleep(long millis) | 在指定的毫秒数内让正在执行的线程休眠 |
| void join() | 等待该线程终止 |
| static void yield() | 线程礼让:暂停当前执行的线程,并执行其他线程 |
| boolean isAlive() | 测试线程是否处于活动状态 |
2.线程停止
1.建议线程正常停止----》利用次数。不建议死循环
2.建议使用标志位----》设置一个标志位 flage
3.不用使用stop或destory 等过时或者JDK 不建议使用的方法
public class TestStop implements Runnable {
//1.设置一个标志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag) {
System.out.println("run...Thread->" + i++);
}
}
//2.设置一个公开的方法停止线程,转换标志位
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 < 1000; i++) {
System.out.println("main" + i);
if (i == 900) {
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("该线程停止了");
}
}
}
}
3.
public class TestSleep implements Runnable {
//票数
private int ticketNums = 10;
@Override
public void run() {
while (true) {
if (ticketNums <= 0) {
break;
}
//模拟延时
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "--->
拿到了第" + ticketNums-- + "票");
}
}
public static void main(String[] args) {
TestSleep testSleep = new TestSleep();
new Thread(testSleep,"小明").start();
new Thread(testSleep,"小红").start();
new Thread(testSleep,"小黄牛").start();
}
}
模拟倒计时 、打印当前系统时间
mport java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep2 {
//模拟倒计时
public static void testDown() throws InterruptedException {
int num = 10;
while (true) {
Thread.sleep(1000);
System.out.println(num--);
if (num == 0) {
break;
}
}
}
//打印当前时间
public static void printNowDate() {
//打印当前系统时间
Date stattTime = new Date(System.currentTimeMillis());
while (true) {
try {
//休眠1秒
Thread.sleep(1000);
//格式化时间,并输出时间
System.out.println(new SimpleDateFormat("yyyy年MM月dd日
HH:mm:ss").format(stattTime));
//更新获取时间
stattTime = new Date(System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
printNowDate();
}
}
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()+"线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
//A线程开始执行(礼让成功)
B线程开始执行
A线程停止执行
B线程停止执行
注意:也可能礼让失败
5.线程强制执行(join)
-
Join 合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞
-
可以想象成插队
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; 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 < 200; i++) {
if (i == 100) {
thread.join();//插队
}
System.out.println("main" + i);
}
}
}
//一般情况VIP线程会在100之前执行完,再进行主线程
6.
1.Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器安装优先级决定应该调度哪个线程来执行。
2.线程的优先级用数字表示,范围从1~10
Thread.MIN_PRIORITY = 1; 最低优先级
Thread.MAX_PRIORITY = 10; 最高优先级
Thread.NOPM_PRIORITY = 5; 普通优先级
使用以下方式改变或获取优先级
getPriority()
setPriority(int xx)
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);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);
//先设置优先级,再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);//10
t4.start();
// t5.setPriority(-1);//通过查看源码发现,设置优先级为-1会报错(小于1)
// t5.start();
// t6.setPriority(11);//通过查看源码发现,设置优先级为11会报错(大于10)
//t6.start();
t5.setPriority(8);
t5.start();
t6.setPriority(7);
t6.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
//线程名字,线程优先级
System.out.println(Thread.currentThread().getName()+"=="+Thread.currentThread().getPriority());
}
}
//main==5
Thread-0==5
Thread-3==10
Thread-4==8
Thread-5==7
Thread-1==1
Thread-2==4
这里t1是Thread-0,它没有设置优先级默认优先级是5
注意:优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用,这都是看CPU的调度。

浙公网安备 33010602011771号