线程状态
一旦进入死亡状态就不能再一次被启动
public class TestState {
public static void main(String[] args) {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(100);
} 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){
state = thread.getState();//更新线程状态
System.out.println(state);//输出状态
}
}
}
…
线程方法
setPriority(int newPriority) //更改线程优先级
-
-
线程休眠
static void sleep(long millis)
-
//sleep(时间)指定当前线程阻塞毫秒数
//sleep存在异常InterruptedException
//sleep时间达到后线程进行就绪状态
//sleep可以模拟网络延时(放大问题的发生性),倒计时
public class TenDown {
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;
}
}
}
}
//打印系统时间
//打印系统时间
public class TenDown1 {
public static void main(String[] args) {
//打印当前时间
Date startTime = new Date(System.currentTimeMillis());//获取当前系统时间
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());//更新时间
} 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;
}
}
}
}
//每个对象都有一个锁,sleep不会释放锁
-
等待该线程终止
void join()
public class TestJoin implements Runnable{ @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println("线程mvp来了"+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 < 500; i++) { if(i==200){ thread.join();//插队 } System.out.println("main"+i); } } }
-
停止当前,执行其他
static void yield()
//礼让不一定成功 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()+"线程停止-———="); } }
-
*使用标志位停止线程
//使用标志位停止线程
public class ThreadStop implements Runnable {
//设置标志位
private boolean flag=true;
//重写run方法
@Override
public void run(){
int i=0;
while(flag){
System.out.println("run --Thread ---"+i++);
}
}
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
ThreadStop threadstop = new ThreadStop();
new Thread(threadstop).start();
//
for (int i = 0; i < 1000; i++) {
System.out.println("main "+i);
if(i==900){
//调用stop方法
threadstop.stop();
System.out.println("线程 停止");
}
}
}
}
- 中断线程
```java
void interrupt()
```
- 查看线程状态
```java
boolean isAlive()
```