javaSE之线程联合

2018-03-26 10:44:48 浏览数 (1)

        首先定义 : 一个线程A在占有CPU资源期间 ,可以让其他线程调用join()和本线程联合。

   嗯哈,像书本这个列子:

           如: B.join();

       我们称A在运行期间联合了B, 如果线程A在占有CPU资源期间一旦联合B线程,那么A线程将立刻

       中断执行,一直等到它联合的线程B执行完毕,A线程再重新排队等待CPU资源,以便恢复执行,

      如果A准备联合的B线程已经结束,那么B.join(),不会产生任何效果。

代码语言:javascript复制
 1 package dialog_color;
 2 
 3 public class Example12_8 
 4 {
 5     public static void main(String args [])
 6     {
 7        ThreadJoin a = new ThreadJoin();
 8        Thread customer = new Thread(a);
 9        Thread cakeMaker = new Thread(a);
10        customer.setName("顾客");
11        cakeMaker.setName("蛋糕");
12        a.setJoinThread(cakeMaker);
13        customer.start();
14     }
15 }
16 class ThreadJoin implements Runnable
17 {
18     Cake cake;
19     Thread joinThread;
20     public void setJoinThread(Thread it)
21     {
22       this.joinThread=it;
23     }
24     public void run() {
25         // TODO Auto-generated method stub        
26       if(Thread.currentThread().getName().equals("顾客"))
27       {
28           System.out.println(Thread.currentThread().getName()
29                    "等待" joinThread.getName() "制作生日蛋糕"); 
30           try { 
31               joinThread.start();
32                joinThread.join();
33             
34         } catch (InterruptedException e) {
35             // TODO Auto-generated catch block
36         //    e.printStackTrace();
37         }
38         System.out.println(Thread.currentThread().getName() "买了" cake.name "价钱: " cake.price);
39       }
40       else if(Thread.currentThread()==joinThread)
41       {
42         System.out.println(joinThread.getName() "开始制作生日蛋糕,请等待......");
43         try {
44             Thread.sleep(2000);
45         } catch (InterruptedException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         }
49         cake=new Cake("生日蛋糕",158);
50         System.out.println(joinThread.getName() "制作完成!");
51       }
52     }
53     
54   class Cake    //内部类  相当于一个结构体) (c/c  )
55   {
56       int price ;
57       String name;
58       Cake(String name ,int price)
59       {
60         this.name=name;
61         this.price=price;
62       }
63   }    
64 }

 效果图:

code:

代码语言:javascript复制
 1 package tes;
 2 /*
 3  * 模拟这样一个过程
 4  * 一个老头去买蛋糕,蛋糕需要先做,
 5  * 老头等待做完,然后付款离开。
 6  * 
 7  * */
 8 public class Example12_9 {
 9 
10     public static void main(String args [])
11     {
12      Thread aa,bb;
13       JoinThread hehe = new JoinThread();
14      aa = new Thread(hehe);
15      bb = new Thread(hehe);
16      aa.setName("老头");
17      bb.setName("蛋糕师傅");
18      hehe.setJoinThread(bb);
19      aa.start();
20     } 
21 }
22 class JoinThread implements Runnable
23 {
24     Thread myThread;
25     cake mycake;
26     public void setJoinThread(Thread name)    {
27         myThread=name;
28     }
29     @Override
30     public void run() {
31         // TODO Auto-generated method stub
32         if(Thread.currentThread().getName().equals("老头"))
33         {
34           print(Thread.currentThread().getName() "等待蛋糕师傅制作蛋糕"
35                    myThread.currentThread().getName() "开始制作蛋糕.....");
36             //开始联合同步
37           myThread.start();
38           try {
39             myThread.join();
40         } catch (InterruptedException e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44           print(myThread.currentThread().getName() "蛋糕做完..."
45                    Thread.currentThread().getName() "买了" mycake.name
46                    " 价钱:" mycake.price);
47         }
48         else if(Thread.currentThread()==myThread)
49         {
50           print(myThread.currentThread().getName() 
51                   "开始制作蛋糕,wait for minutes !");
52           try {
53             Thread.sleep(2500);
54         } catch (InterruptedException e) {
55             // TODO Auto-generated catch block
56             e.printStackTrace();
57         }
58          print("蛋糕做完了"); 
59          mycake = new cake("蛋糕",12.8);
60         }
61     }
62    private  void print(String name){
63      System.out.println(name);
64    }
65     class cake
66     {
67         String name;
68         double price;
69         cake(String name,double price){
70           this.name=name;
71           this.price=price;
72         }
73     }
74 }

0 人点赞