多线程详解_2

p11-p19

1: 线程状态

 

2: 停止状态

 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data  5.24
 6  * since 1.8
 7  * version 1.0
 8  * Description  停止线程
 9  */
10 public class Test8 implements Runnable{
11     private boolean flag = true;
12     @Override
13     public void run() {
14         int i = 0;
15         while (flag){
16             System.out.println("线程结束" + i++);
17         }
18     }
19     public void stop()
20     {
21         flag = false;
22     }
23 
24     public static void main(String[] args) {
25         Test8 t =new Test8();
26         new Thread(t).start();
27         for (int i = 0; i < 1000; i++) {
28             System.out.println("main"+i);
29             if(i == 900){
30                 t.stop();
31             }
32         }
33     }
34 }

3: 线程休眠

 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data 5.24
 6  * since 1.8
 7  * version 1.0
 8  * Description  线程休眠
 9  */
10 public class Test9 implements Runnable{
11     private int ticketNums = 10;
12 
13     @Override
14     public void run() {
15         while(true){
16             if(ticketNums <= 0){
17                 break;
18             }
19             //模拟延时
20             try {
21                 Thread.sleep(100);
22             } catch (InterruptedException e) {
23                 e.printStackTrace();
24             }
25             System.out.println(Thread.currentThread().getName() + "拿到了第:" + ticketNums-- + "张票");
26         }
27     }
28 
29     public static void main(String[] args) {
30         Test9 t = new Test9();
31         new Thread(t).start();
32         new Thread(t).start();
33 
34     }
35 }
 1 package Thread;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 /**
 7  * author liulei
 8  * data  5.24
 9  * since 1.8
10  * version 1.0
11  * Description  模拟倒计时
12  */
13 public class Test10 {
14 
15     public static void main(String[] args) throws InterruptedException {
16         new Thread(new tt()).start();
17         Date startTime = new Date(System.currentTimeMillis());//获取系统当前时间
18         while (true){
19             Thread.sleep(1000);
20             System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
21             startTime = new Date(System.currentTimeMillis());//更新
22 
23         }
24     }
25 
26 
27 }
28 class tt implements Runnable{
29     private static  boolean flag  = true;
30     @Override
31     public void run() {
32         int clock = 10;
33         while (flag){
34             try {
35                 Thread.sleep(100);
36             } catch (InterruptedException e) {
37                 e.printStackTrace();
38             }
39             System.out.println("倒计时" + clock--);
40             if(clock <= 0){
41                 System.out.println("倒计时结束");
42                 stop();
43             }
44         }
45     }
46     public  static void stop(){
47         flag = false;
48     }
49 }

4: 线程礼让和插队

 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data   5.24
 6  * since 1.8
 7  * version 1.0
 8  * Description  线程礼让
 9  */
10 public class Test11 {
11     public static void main(String[] args) {
12         Myyeild my = new Myyeild();
13         new Thread(my,"a").start();
14         new Thread(my,"b").start();
15 
16     }
17 
18 }
19 class Myyeild implements Runnable{
20 
21     @Override
22     public void run() {
23         System.out.println(Thread.currentThread().getName()+"线程开始执行");
24         Thread.yield();
25         System.out.println(Thread.currentThread().getName()+"线程停止执行");
26     }
27 }
 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data
 6  * since 1.8
 7  * version 1.0
 8  * Description 插队
 9  */
10 public class Test12 implements Runnable{
11     @Override
12     public void run() {
13         for (int i = 0; i < 500; i++) {
14             System.out.println("线程VIP来了" + i);
15         }
16     }
17 
18     public static void main(String[] args) throws InterruptedException {
19         //启动我们的线程
20         Test12 t = new Test12();
21         Thread a = new Thread(t);
22         a.start();
23         for (int i = 0; i < 500; i++) {
24             if ( i ==200){
25                a.join();
26             }
27             System.out.println("main" + i);
28         }
29     }
30 }

5: 线程状态观测

 1 package Thread;
 2 
 3 import java.sql.SQLOutput;
 4 
 5 /**
 6  * author liulei
 7  * data  5.24
 8  * since 1.8
 9  * version 1.0
10  * Description  线程状态
11  */
12 public class Test13 {
13     public static void main(String[] args) throws InterruptedException {
14         Thread thread = new Thread(()->{
15             for (int i = 0; i < 5; i++) {
16                 try {
17                     Thread.sleep(1000);
18                 } catch (InterruptedException e) {
19                     e.printStackTrace();
20                 }
21             }
22             System.out.println("//////");
23         });
24         //观察状态
25         Thread.State state = thread.getState();
26         System.out.println(state);
27         //观察启动后
28         thread.start();//启动线程
29         state = thread.getState();
30         System.out.println(state);
31         while (state != Thread.State.TERMINATED){
32             state = thread.getState();
33             System.out.println(state);
34             Thread.sleep(100);
35             state = thread.getState();
36             System.out.println(state);
37         }
38     }
39 
40 }

6: 线程优先级

 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data   5.24
 6  * since 1.8
 7  * version 1.0
 8  * Description  测试线程优先级
 9  */
10 public class test15 {
11     public static void main(String[] args) {
12         System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
13         Mypriority mypriority = new Mypriority();
14         Thread t1 = new Thread(mypriority);
15         Thread t2 = new Thread(mypriority);
16         Thread t3 = new Thread(mypriority);
17         Thread t4 = new Thread(mypriority);
18         Thread t5 = new Thread(mypriority);
19         Thread t6 = new Thread(mypriority);
20         t1.start();
21 
22         t2.setPriority(1);
23         t2.start();
24 
25         t3.setPriority(4);
26         t3.start();
27 
28         t4.setPriority(Thread.MAX_PRIORITY);
29         t4.start();
30 
31         //t5.setPriority(-1);  //报错范围是0-10
32         //t5.start();
33 
34         //t6.setPriority(11);
35         //t6.start();
36 
37     }
38 
39 
40 }
41 class Mypriority implements Runnable{
42 
43     @Override
44     public void run() {
45         System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
46     }
47 }

7: 守护线程

 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data  5.24
 6  * since 1.8
 7  * version 1.0
 8  * Description  测试守护线程
 9  */
10 public class Test16 {
11     public static void main(String[] args) {
12         God g = new God();
13         your r =new your();
14         Thread thread = new Thread(g);
15         thread.setDaemon(true);
16         thread.start();
17         new Thread(r).start();
18     }
19 }
20 class God implements Runnable{
21 
22     @Override
23     public void run() {
24         while (true){
25             System.out.println("上帝保佑你");
26         }
27     }
28 }
29 class your implements Runnable{
30 
31     @Override
32     public void run() {
33         for (int i = 0; i < 36; i++) {
34             System.out.println("你一声快乐的生活着");
35         }
36     }
37 }

8: 线程同步与三个不安全线程

 1 package Thread;
 2 
 3 /**
 4  * author liulei
 5  * data
 6  * since 1.8
 7  * version 1.0
 8  * Description 不安全线程一
 9  */
10 //不安全的买票
11 public class Test17 {
12     public static void main(String[] args) {//出现同一张票被多次购买
13         BuyTicket t = new BuyTicket();
14         new Thread(t,"a").start();
15         new Thread(t,"b").start();
16         new Thread(t,"c").start();
17     }
18 }
19 class BuyTicket implements Runnable{
20     private int ticketNum = 10;
21     Boolean flag =true;//外部停止方式
22     @Override
23     public void run() {
24         try {
25             Thread.sleep(100);
26         } catch (InterruptedException e) {
27             e.printStackTrace();
28         }
29         while (flag){
30             buy();
31         }
32     }
33     private void buy(){
34         if(ticketNum <= 0){
35             flag = false;
36             return;
37         }
38         System.out.println(Thread.currentThread().getName() + "拿到" + ticketNum--);
39 
40     }
41 
42 
43 }
package Thread;

/**
 * author liulei
 * data
 * since 1.8
 * version 1.0
 * Description  不安全线程二
 */
public class Test18 {
    public static void main(String[] args) {
        Account account = new Account(100,"结婚基金");
        Drawing you = new Drawing(account, 50, "");
        Drawing girl = new Drawing(account, 100, "");
        you.start();
        girl.start();//可能导致多取钱
    }
}
class Account{
    int money;//余额
    String name;//卡名

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}
//银行,模拟取款
class Drawing extends Thread{
    Account account;//账户
    //取了多少钱
    int drawingMoney;
    //现在手里有多少钱
    int nowmoney;


    public Drawing(Account account, int drawingMoney, String name) {
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;

    }

    @Override
    public void run() {
        if(account.money-drawingMoney<0){
            System.out.println(Thread.currentThread().getName() + "钱不够取不了");
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        account.money -=drawingMoney;
        nowmoney += drawingMoney;
        System.out.println(account.name + "余额为" + account.money);
        System.out.println(this.getName()+"手里的钱" + nowmoney);

    }
}
 1 package Thread;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /**
 7  * author liulei
 8  * data
 9  * since 1.8
10  * version 1.0
11  * Description  不安全线程3
12  */
13 public class Test19 {
14     public static void main(String[] args) throws InterruptedException {
15         List<String> list = new ArrayList<String>();
16         for (int i = 0; i < 10000; i++) {
17             new Thread(()->{
18                 list.add(Thread.currentThread().getName());
19             }).start();
20         }
21         Thread.sleep(100);
22         System.out.println(list.size());//总数可能少于10000
23     }
24 
25 }

猜你喜欢

转载自www.cnblogs.com/henuliulei/p/12952922.html