多線程練習

寫兩個線程,其中一個線程打印1-52,另外一個線程打印A-Z,打印順序應該是12A34B56C......5152Z。多線程

該習題須要用到多線程通訊的知識。this

思路分析:spa

把打印數字的線程稱爲線程N,打印字母的線程稱爲線程L.線程

1.線程N完成打印後,須要等待,通知線程L打印;同理,線程L打印後,也須要等待,並通知線程N打印code

線程的通訊能夠利用Object的wait和notify方法。對象

2.兩個線程在執行各自的打印方法的時候,不該該被打斷,因此把打印方法設置成同步的方法。blog

3.兩個線程什麼時候中止打印?當兩個線程的打印方法都執行了26次的時候。get

 

實現:同步

1.PrintStr類,用來完成打印,擁有printNumber和printLetter兩個同步方法it

 1 package pratise.multithreading;
 2 
 3 public class      {
 4     
 5     private int flag=0;
 6     private int beginIndex=1;
 7     private int beginLetter=65;
 8     
 9     private int nCount=0;
10     private int lCount=0;
11     
12     public int getnCount()
13     {
14         return nCount;
15     }
16     
17     public int getlCount()
18     {
19         return lCount;
20     }
21     
22     public synchronized void printNumber() 
23     {
24         try {
25                 if(flag==0)
26                 {
27                     nCount++;
28                     System.out.print(beginIndex);
29                     System.out.print(beginIndex+1);
30                     beginIndex+=2;
31                     //改標誌位
32                     flag++;
33                     //喚醒另外一個線程
34                     notify();
35                 }else
36                 {
37                     wait();
38                 }
39         } catch (InterruptedException e) {
40             e.printStackTrace();
41         }
42     
43     }
44     
45     public synchronized void printLetter()
46     {
47         try {
48                 if(flag==1)
49                 {
50                     lCount++;
51                     char letter=(char)beginLetter;
52                     System.out.print(String.valueOf(letter));
53                     beginLetter++;
54                     flag--;
55                     notify();
56                 }else
57                 {
58                     wait();
59                     
60                 }
61         } catch (InterruptedException e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         }
65         
66             
67     }
68 
69 }

 

2.兩個線程類,分別包含一個PrintStr對象

 1     package pratise.multithreading;
 2     
 3     public class PrintNumberThread extends Thread {
 4         private PrintStr ps;
 5         public PrintNumberThread(PrintStr ps)
 6         {
 7             this.ps=ps;
 8         }
 9     
10         public void run()
11         {
12             
13             while(ps.getnCount()<26)
14             {
15                 ps.printNumber();
16             }
17         }
18     }
 1 package pratise.multithreading;
 2 
 3 public class PrintLetterThread extends Thread {
 4     private PrintStr ps;
 5     public PrintLetterThread(PrintStr ps)
 6     {
 7         this.ps=ps;
 8     }
 9 
10     public void run()
11     {
12         while(ps.getlCount()<26)
13         {
14             ps.printLetter();
15         }
16     }
17 }

 

3.含有main方法的類,用來啓動線程。

 1 package pratise.multithreading;
 2 
 3 public class PrintTest {
 4 
 5     public static void main(String[] args) {
 6         PrintStr ps=new PrintStr();
 7         new PrintNumberThread(ps).start();
 8         new PrintLetterThread(ps).start();
 9     }
10 
11 }
相關文章
相關標籤/搜索