一、Thread类及常见方法
Thread 类是 JVM 用来管理线程的一个类,换句话说,每个线程都有一个唯一的 Thread 对象与之关联;每个执行流,也需要有一个对象来描述,而 Thread 类的对象 就是用来描述一个线程执行流的,JVM 会将这些 Thread对象组织起来,用于线程调度,线程管理。1.1 Thread的常见构造方法
Thread t1 = new Thread(); Thread t2 = new Thread(new MyRunnable()); Thread t3 = new Thread("这是我的名字"); Thread t4 = new Thread(new MyRunnable(), "这是我的名字"); 1234
1.2 Thread的几个常见属性
public class Demo8 { public static void main(String[] args) { Thread t = new Thread(() -> { while (true) { System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }, "这是俺的线程"); t.start(); System.out.println(t.getId());//12 System.out.println(t.getName());//这是俺的线程 System.out.println(t.getPriority());//5 System.out.println(t.getState());//RUNNABLE System.out.println(t.isDaemon());//false System.out.println(t.isAlive());//true System.out.println(t.isInterrupted());//false } }
123456789101112131415161718192021222324251.3 启动一个线程
通过覆写 run 方法创建一个线程对象,但线程对象被创建出来并不意味着线程就开始运行了;调用 start 方法, 才真的在操作系统的底层创建出一个线程;1.4 中断一个线程
通过共享的标记来进行沟通public class Demo10 { //自己定义标记位,作为线程是否结束的标记 private static boolean isQuit = false; public static void main(String[] args) { Thread t = new Thread(() -> { while (!isQuit) { System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("t 线程执行完了"); }); t.start(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } isQuit = true; System.out.println("设置让 t 线程结束!"); } }
1234567891011121314151617181920212223242526272829 调用 interrupt() 方法来通知public class Demo11 { public static void main(String[] args) { Thread t = new Thread(() -> { while (!Thread.currentThread().isInterrupted()) {//标准库自带的标志位 System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { // e.printStackTrace(); // [方式一] 立即结束线程 break; // [方式二] 啥都不做, 不做理会. 线程继续执行 // [方式三] 线程稍后处理 // Thread.sleep(1000); // break; } } System.out.println("t 线程执行完了"); }); t.start(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } t.interrupt();//在主线程中,中断这个线程,设置标志位为true System.out.println("设置让 t 线程结束!"); } }
12345678910111213141516171819202122232425262728293031323334351.5 等待一个线程
public class Demo12 { public static void main(String[] args) { Thread t = new Thread(() -> { for (int i = 0; i < 5; i++) { System.out.println("hello thread!"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); t.start(); System.out.println("main 线程 join 之前"); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("main 线程 join 之后"); } }
12345678910111213141516171819202122231.6 获取当前线程的引用
public class ThreadDemo { public static void main(String[] args) { Thread thread = Thread.currentThread(); System.out.println(thread.getName()); } } 123456
1.7 休眠当前线程
public class ThreadDemo { public static void main(String[] args) throws InterruptedException { System.out.println(System.currentTimeMillis()); Thread.sleep(3 * 1000); System.out.println(System.currentTimeMillis()); } } 1234567
二、线程的状态
NEW: 安排了工作, 还未开始行动;RUNNABLE: 可工作的,又可以分成正在工作中和即将开始工作;BLOCKED: 这几个都表示排队等着其他事情;WAITING: 这几个都表示排队等着其他事情;TIMED_WAITING: 这几个都表示排队等着其他事情;TERMINATED: 工作完成了;public class Demo13 { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(() -> { // for (int i = 0; i < 10000_0000; i++) { // // 啥都不干 // } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } }); // t 开始之前, 得到的就是 NEW System.out.println(t.getState()); t.start(); Thread.sleep(50); // t 正在工作, 得到的是 RUNNABLE System.out.println(t.getState()); t.join(); // t 结束之后, 得到的状态 TERMINATED System.out.println(t.getState()); } }
123456789101112131415161718192021222324252627282930相关知识
多线程并行与退出
幼儿园季节活动教案:初步了解四季轮换顺序
成都五月花劳动职业技能培训学校
多线程进阶 => JUC并发编程(已完结,但图片未提交)
月季抗白粉病育种初步研究
无结构土壤透气性的初步研究
广州地区花境植物资源初步研究
城市绿化树木减弱噪声效应的初步观察
牡丹、芍药开花生理及花衰老控制的初步研究的开题报告
射阳县五年土壤肥力监测的初步效果
网址: 【多线程】初步了解(2) https://m.huajiangbk.com/newsview1054870.html
上一篇: Thread类 |
下一篇: python学习笔记(二十三) |