一个线程可以在给定的时间点处于一个状态,这些状态是不反映任何操作系统线程状态的虚拟机状态
getState()方法查看线程的状态
状态标识符 | 状态描述 |
---|---|
NEW | 尚未启动的线程处于此状态 |
RUNNABLE | 在Java虚拟机中执行的线程处于此状态 |
BLOCKED | 被阻塞等待监视器锁定的线程处于此状态 |
WAITING | 正在等待另一个线程执行特定动作的线程处于此状态 |
TIMED_WAITING | 正在等待另一线程执行动作达到指定等待时间的线程处于此状态 |
TERMINATED | 已退出的线程处于此状态 |
public class Hello{ public static void main(String[] args) { //Lambda表达式创建线程对象并重写run()方法 Thread thread = new Thread(() -> { for (int i = 0; i < 5; i++) { try { //sleep()方法是类方法 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("这是run()方法"); }); //getState()方法启动前观察状态(NEW) System.out.println(thread.getState()); //启动后观察状态(RUNNABLE) thread.start(); System.out.println(thread.getState()); //只要线程不终止,就打印出线程状态 //getState()方法返回值是Thread.State类型 while (thread.getState() != Thread.State.TERMINATED){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //更新线程状态 System.out.println(thread.getState()); } } }
public class Hello implements Runnable { //1.设置一个标志位 boolean flag = true; @Override public void run() { int i = 0; while (flag){ System.out.println("线程运行了" + i++ + "次"); } } //2.设置一个公开的方法停止线程,转换标志位 public void stop(){ flag = false; } public static void main(String[] args) { Hello hello = new Hello(); new Thread(hello).start(); for (int i = 0; i <= 1000; i++) { System.out.println("main方法运行了" + i + "次" ); if (i == 900){ //调用stop()方法切换标志位,让线程停止 hello.stop(); System.out.println("该结束了"); break; } } } }
练习:模拟延时
public class Hello implements Runnable { int num = 10; @Override public void run() { while (true) { //模拟网络延时,放大问题的发生性 try { //sleep()方法休眠 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } if (num <= 0) { break; } System.out.println(Thread.currentThread().getName() + "抢到了第" + num-- +"张票"); } } public static void main(String[] args) { Hello hello = new Hello(); new Thread(hello, "老师").start(); new Thread(hello, "学生").start(); new Thread(hello, "黄牛").start(); } }
练习:当前系统时间倒计时
import java.time.LocalTime; public class Hello { public static void TestSleep() throws InterruptedException { for (int i = 10; i > 0; i--) { LocalTime now = LocalTime.now(); System.out.print(now); Thread.sleep(1000); //刷新打印区 System.out.print('\r'); } } public static void main(String[] args) throws InterruptedException { TestSleep(); } }
public class Hello implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "线程开始执行!"); //yield()方法线程礼让 Thread.yield(); System.out.println(Thread.currentThread().getName() + "线程结束!"); } public static void main(String[] args) { Hello hello = new Hello(); new Thread(hello, "学生").start(); new Thread(hello, "老师").start(); } }
public class Hello implements Runnable { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "线程强制执行" + i + "次"); } } public static void main(String[] args) throws InterruptedException { Hello hello = new Hello(); //join()方法需要具体的对象名调用,因此显示创建线程对象 Thread thread = new Thread(hello, "VIP"); thread.start(); for (int i = 0; i < 100; i++) { if (i == 10){ //join(0方法线程插队强制执行,执行完以后才会执行其他线程 thread.join(); } System.out.println("主线程执行" + i + "次"); } } }