多线程

 1 /**Thread中的常用方法:
 2  *1:start()方法:启动线程,调用当前线程搞得run方法
 3  *2:run()方法:通常需要重写Thread中的此方法,将创建的线程要执行的操作声明在此方法中
 4  *3:currentThread():静态方法,返回当前代码的线程
 5  *4:getName:获取当前线程的名字
 6  *5:setName:设置当前线程名字
 7  *6:yield():释放当前cpu的执行权
 8  *7:join():线程a中使用join方法,代表线程a暂时终止等线程b先完成,线程a暂时处于堵塞状态
 9  *8:stop():当执行此方法时,强制结束当前线程
10  *9:sleep(毫秒):让当前线程睡眠指定的时间毫秒,在指定的时间内,当前线程处于堵塞状态
11 */ 12 public class Demo extends Thread{ 13 @Override 14 public void run() { 15 for (int i = 0; i < 1000; i++) { 16 setName("分线程"); 17 System.out.println(getName()+i); 18 try { 19 sleep(100); 20 } catch (InterruptedException e) { 21 e.printStackTrace(); 22 } 23 try { 24 if (i==50) 25 join(); 26 } catch (InterruptedException e) { 27 e.printStackTrace(); 28 } 29 } 30 } 31 32 public static void main(String[] args) { 33 Demo demo=new Demo(); 34 demo.start(); 35 for (int i = 0; i < 200; i++) { 36 try { 37 sleep(1); 38 } catch (InterruptedException e) { 39 e.printStackTrace(); 40 } 41 currentThread().setName("主线程"); 42 System.out.println(currentThread().getName()+i); 43 currentThread().stop(); 44 // if (i==3) { 45 // try { 46 // demo.join(); 47 // } catch (InterruptedException e) { 48 // e.printStackTrace(); 49 // } 50 // } 51 } 52 } 53 }
 1 package JavaSenior.ThreadTest;
 2 
 3 import com.sun.org.apache.xpath.internal.operations.Or;
 4 
 5 import java.text.BreakIterator;
 6 
 7 /**
 8  * 创建线程的两种方式
 9  *1:通过子类继承Thread类,重写run方法,创建对象调用start方法
10  *2:通过实现类继承Runnable接口,去实现Runnable中的抽象run方法,创建实现类对象作为参数传入
11  *  创建的Thread类对象中Thread thread=new Thread(实现类对象);进行调用start方法创建线程
12  *
13  *开发中优先选择Runnable接口方式创建
14  * 原因:1:可以实现多继承,避免了类的单继承的缺点
15  *      2:更适合用于多个线程共享数据的情况
16  */
17 //方式1
18 public class Demo2 extends Thread {
19     private  static int ticket=100;
20     @Override
21     public void run() {
22         while (true) {
23             if (ticket > 0) {
24                 System.out.println(Thread.currentThread().getName() + "\t" + ticket);
25                 ticket--;
26             } else {
27                 break;
28             }
29         }
30     }
31     public static void main(String[] args) {
32         Demo2 demo2=new Demo2();
33         Demo2 demo3=new Demo2();
34         Demo2 demo4=new Demo2();
35         demo2.setName("线程2");
36         demo3.setName("线程3");
37         demo4.setName("线程4");
38         demo2.start();
39         demo3.start();
40         demo4.start();
41     }
42 }
43 
44 //方式2
45 class Demo3 implements Runnable {
46     private int ticket = 100;
47 
48     @Override
49     public void run() {
50         while (true) {
51             if (ticket > 0) {
52                 System.out.println(Thread.currentThread().getName() + "\t" + ticket);
53                 ticket--;
54             } else {
55                 break;
56             }
57         }
58     }
59 
60     public static void main(String[] args) {
61         Demo3 demo3 = new Demo3();
62         Thread thread = new Thread(demo3);
63         Thread thread1 = new Thread(demo3);
64         Thread thread2 = new Thread(demo3);
65         thread.setName("线程0");
66         thread1.setName("线程1");
67         thread2.setName("线程2");
68         thread.start();
69         thread1.start();
70         thread2.start();
71     }
72 }
线程优先级:getPriority();获取线程优先级,setPriority(int p):
设置线程优先级MAX==10.MIN==1,默认==5;
posted @ 2021-10-12 19:01  tiiiiii  阅读(40)  评论(0)    收藏  举报