Multithread Exercise

Write two threads, one of which prints 1-52 and the other A-Z. The order of printing should be 12A34B56C... 5152Z.

This exercise requires knowledge of multithreaded communication.

Idea analysis:

Threads that print numbers are called threads N, and threads that print letters are called threads L.

1. When thread N finishes printing, it needs to wait and notify thread L to print. Similarly, thread L also needs to wait after printing and notify thread N to print.

Thread communication can take advantage of Object wait and notify methods.

2. Two threads should not be interrupted when executing their respective printing methods, so the printing methods should be set to synchronous methods.

3. When do two threads stop printing? When the printing methods of both threads are executed 26 times.

 

Realization:

1.PrintStr class, used to complete printing, with printNumber and printLetter two synchronization methods

 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                     //Change flag bit
32                     flag++;
33                     //Wake up another thread
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. Two thread classes, each containing a PrintStr object

 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. Classes with main methods to start threads.

 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 }

Tags: Java

Posted on Sun, 06 Oct 2019 11:00:29 -0700 by jimjack145