【1】线程声明周期:线程开始--》线程消亡
【2】线程经历哪些阶段:

(1)start() : 启动当前线程,表面上调用start方法,实际在调用线程里面的run方法;
(2)run() : 线程类 继承 Thread类 或者 实现Runnable接口的时候,都要重新实现这个run方法, run方法里面是线程要执行的内容;
(3)currentThread :Thread类中一个静态方法:获取当前正在执行的线程;
(4)setName 设置线程名字;
(5)getName 读取线程名字。
【1】同优先级别的线程,采取的策略就是先到先服务,使用时间片策略;
【2】如果优先级别高,被CPU调度的概率就高;
【3】级别:1-10 默认的级别为5。

代码示例:
- package test6_;
-
- /**
- * @Auther: zhoulz
- * @Description: test6_
- * @version: 1.0
- */
- public class TestThread01 extends Thread {
- @Override
- public void run() {
- for (int i = 1; i <= 10; i++) {
- System.out.println(i);
- }
- }
- }
- //再创建一个线程
- class TestThread02 extends Thread{
- @Override
- public void run() {
- for (int i = 20; i <= 30; i++) {
- System.out.println(i);
- }
- }
- }
- //测试类
- class Test1{
- public static void main(String[] args) {
- //创建两个子线程,让这两个子线程争抢资源:
- TestThread01 t1 = new TestThread01();
- t1.setPriority(1);//优先级别低
- t1.start();
-
- TestThread02 t2 = new TestThread02();
- t2.setPriority(10);//优先级别高
- t2.start();
- }
- }
join方法:当一个线程调用了join方法,这个线程就会先被执行,它执行结束以后才可以去执行其余的线程。
注意:必须先start,再join才有效。
代码示例:
- package test6_;
-
- /**
- * @Auther: zhoulz
- * @Description: test6_
- * @version: 1.0
- */
- public class TestThread extends Thread {
- public TestThread(String name){
- super(name);
- }
- @Override
- public void run() {
- for (int i = 1; i <= 10; i++) {
- System.out.println(this.getName()+"---"+i);
- }
- }
- }
-
- //测试类
- class Test{
- public static void main(String[] args) throws InterruptedException {
- for (int i = 1; i <= 100; i++) {
- //System.out.println("main---"+i);
- if (i == 6){ //等于6的时候才创建
- //创建子线程:
- TestThread tt = new TestThread("子线程");
- tt.start();
- tt.join();//“半路杀出个程咬金”
- }
- System.out.println("main---"+i);
- }
- }
- }
运行结果:

可见:如果出现 join()方法的话,会把别的方法置于阻塞状态。
【1】sleep : 人为的制造阻塞事件
- package test6_;
-
- /**
- * @Auther: zhoulz
- * @Description: test6_
- * @version: 1.0
- */
- public class Test3_sleep {
- public static void main(String[] args) throws InterruptedException {
- //5秒钟之后在输出 "000000000"
- System.out.println(System.currentTimeMillis());
- Thread.sleep(5000);
- System.out.println("000000000");
- System.out.println(System.currentTimeMillis());
- }
- }
【2】案例:完成秒表功能
- package test6_;
-
- import javax.xml.crypto.Data;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- /**
- * @Auther: zhoulz
- * @Description: test6_
- * @version: 1.0
- */
- public class Test4_sleep_2 {
- public static void main(String[] args) throws InterruptedException {
- //上面时间的输出格式不喜欢
- //2、定义一个时间格式 —— 定义一次就行了
- DateFormat df = new SimpleDateFormat("HH:mm:ss");
-
- while (true){ //想要一直打印时间
- //1、获取当前时间:
- Date d = new Date();
- //System.out.println(d); // Wed Oct 19 16:05:44 CST 2022
-
- //3、按照上面定义的格式将Date类型转为指定格式的字符串:
- System.out.println(df.format(d));
- //隔1秒打印一次
- Thread.sleep(1000);
- }
- }
- }
【1】设置伴随线程
将子线程设置为主线程的伴随线程,主线程停止的时候,子线程也不要继续执行了。
案例:皇上 --》驾崩 ---》妃子陪葬
代码示例:
- package test6_.Test5_setDaemon;
-
- /**
- * @Auther: zhoulz
- * @Description: test6_.Test5_setDaemon
- * @version: 1.0
- */
- public class TestThread extends Thread {
- @Override
- public void run() {
- for (int i = 1; i <= 1000; i++) {
- System.out.println("子线程---"+i);
- }
- }
- }
- //测试类
- class Test{
- public static void main(String[] args) {
- //创建并启动子线程:
- TestThread tt = new TestThread();
- tt.setDaemon(true);//设置伴随线程 注意:先设置,再启动
- tt.start();
-
- //主线程中,还要输出1-10的数字:
- for (int i = 1; i <= 10; i++) {
- System.out.println("main---"+i);
- }
- }
- }
结果:

代码示例:
- package test6_;
-
- /**
- * @Auther: zhoulz
- * @Description: test6_
- * @version: 1.0
- */
- public class Test6_stop {
- public static void main(String[] args) {
- for (int i = 1; i <= 100; i++) {
- //现要求:该程序被迫终止
- if (i == 6){
- Thread.currentThread().stop();//过期方法,不建议使用
- }
- System.out.println(i);
- }
- }
- }