javaSE 17多线程01

 1 package LS_进阶04_线程_进程_程序;
 2 
 3 /**
 4  * 程序:未运行的软件,比如QQ
 5  * 进程:运行的软件,比如打开的QQ
 6  * 线程:进程里面的最小执行单元,比如聊天功能,发送文件功能
 7  */
 8 
 9 public class TestThead01 {
10     public static void main(String[] args) {
11         //获取当前的线程
12         Thread t = Thread.currentThread();
13         //          线程名称,优先级(默认是5),所属线程池的名称
14         //Thread[main,5,main]
15         System.out.println(t);
16 
17         //设置线程名字
18         t.setName("主线程");
19         System.out.println(t);
20         //设置优先级
21         t.setPriority(10);
22         System.out.println(t);
23         //获取线程池的名称
24         System.out.println(t.getThreadGroup());
25 
26         //主线程睡眠,单位是毫秒,1秒等于1000毫秒
27         try {
28             Thread.sleep(1000);
29         } catch (InterruptedException e) {
30             e.printStackTrace();
31         }//这个会导致:程序运行到这时会等一秒在输出
32 
33 
34         System.out.println("main方法结束");
35     }
36 }
01
 1 package LS_进阶04_线程_进程_程序;
 2 
 3 public class TestThread02 {
 4     public static void main(String[] args) {
 5        //Thread[Thread-0,5,main]
 6         //Thread-0来自Thread里面的构造方法
 7         MyThread01 t1 = new MyThread01();
 8         System.out.println(t1);
 9 
10 
11 //        MyThread02 t2 = new MyThread02();
12 //        //LS_进阶04_线程_进程_程序.MyThread02@7ef20235
13 //        //是因为继承了Object类,里面的toString方法实现的
14 //        System.out.println(t2);
15 
16         //创建线程对象
17         MyThread02 thread = new MyThread02();
18 //        System.out.println(thread);
19         //将线程对象封装成线程
20         Thread t2 = new Thread(thread);
21         System.out.println(t2);
22 
23 
24         //启动线程
25         //让线程进入就绪状态(没有其他功能)
26         //抢夺cpu资源,执行run方法
27         t1.start();
28         t2.start();
29 
30         //调用方法,先输出t1.然后t2
31 //        t1.run();
32 //        t2.run();
33 
34         Thread t3 = new Thread(new Runnable() {
35             @Override
36             public void run() {
37                 System.out.println("t3.start");
38             }
39         });
40         t3.start();
41     }
42 }
43 
44 //继承thread类创建线程
45 //重写run方法
46 class MyThread01 extends Thread{
47 
48     @Override
49 //    public void run() {
50 //        System.out.println("继承Thread类");
51 //    }
52     public void run(){
53         for(int i = 0;i<100;i++){
54             System.out.println("t1--->"+i);
55         }
56     }
57 }
58 
59 //实现Runnable接口创建线程
60 //重写run方法
61 class MyThread02 implements Runnable{
62 
63     @Override
64 //    public void run() {
65 //        System.out.println("实现Runnable接口");
66 //    }
67     public void run(){
68         for(int i = 0;i<100;i++){
69             System.out.println("t2--->"+i);
70         }
71     }
72 }
02
 1 package LS_进阶04_线程_进程_程序;
 2 
 3 public class TestThread03 {
 4     public static void main(String[] args) {
 5       Thread t1 = new Thread(new MyThread01());
 6       Thread t2 = new Thread(new MyThread02());
 7 
 8       //设置优先级
 9         //1-10:10最大
10         //只是表示抢到资源的概率变大了,不是一定抢到
11         t1.setPriority(1);
12         t2.setPriority(10);
13 
14         //启动线程
15         t1.start();
16         //主线程睡眠
17 //        try {
18 //            Thread.sleep(1000*2);
19 //        } catch (InterruptedException e) {
20 //            e.printStackTrace();
21 //        }
22 
23         t2.start();
24         //判断进程是不是存在
25         System.out.println(t1.isAlive());
26     }
27 }
28 
29 class Thread01 implements Runnable{
30 
31     @Override
32     public void run() {
33         for(int i =0;i<100;i++){
34             System.out.println("t1-->"+i);
35             Thread.yield();//让出cpu资源,t1,t2继续抢夺
36 
37         }
38     }
39 }
40 
41 class Thread02 implements Runnable{
42 
43     @Override
44     public void run() {
45         for(int i =0;i<100;i++){
46             System.out.println("t2-->"+i);
47         }
48     }
49 }
03
 1 package LS_进阶04_线程_进程_程序;
 2 
 3 public class TestThread04 {
 4     public static void main(String[] args) {
 5         //创建线程
 6         Thread t1 = new Thread(new Runnable() {
 7             @Override
 8             public void run() {
 9                 for (int i =0;i<100;i++){
10                     System.out.println("T1--->"+i);
11                 }
12             }
13         });
14         //创建线程
15         Thread t2 = new Thread(new Runnable() {
16             @Override
17             public void run() {
18                 for (int i =0;i<100;i++){
19                     System.out.println("T2--->"+i);
20                     if(i == 50){
21                         try {
22                             t1.join();//t2让出资源,等t1执行完成,t2在执行
23                         } catch (InterruptedException e) {
24                             e.printStackTrace();
25                         }
26                     }
27                 }
28             }
29         });
30 
31         //启动线程
32         t1.start();
33         t2.start();
34     }
35 }
04
 1 package LS_进阶04_线程_进程_程序;
 2 
 3 public class TestThread05 {
 4     public static void main(String[] args) {
 5         MyThread myThread = new MyThread();
 6         Thread t1 = new Thread(myThread);
 7         t1.start();
 8         try {
 9             Thread.sleep(1000*5);
10         } catch (InterruptedException e) {
11             e.printStackTrace();
12         }
13         myThread.flag = false;
14     }
15 }
16 
17 
18 class MyThread implements Runnable{
19     //设置标志位
20     boolean flag = true;
21     @Override
22     public void run() {
23         for(int i =0;i<100;i++){
24         if(i==5){
25             //抛出异常
26             throw new NullPointerException("空指针异常");
27         }
28         if(flag){
29                 System.out.println("t1-->"+i);
30                 try {
31                     Thread.sleep(1000);
32                 } catch (InterruptedException e) {
33                     e.printStackTrace();
34                 }
35             }
36 
37         }
38 
39     }
40 }
05

 

posted @ 2021-02-06 14:10  赚钱买个打印机  阅读(87)  评论(0编辑  收藏  举报