当前位置: 移动技术网 > IT编程>开发语言>Java > 多线程练习

多线程练习

2019年08月11日  | 移动技术网IT编程  | 我要评论

写两个线程,其中一个线程打印1-52,另一个线程打印a-z,打印顺序应该是12a34b56c......5152z。

该习题需要用到多线程通信的知识。

思路分析:

把打印数字的线程称为线程n,打印字母的线程称为线程l.

1.线程n完成打印后,需要等待,通知线程l打印;同理,线程l打印后,也需要等待,并通知线程n打印

线程的通信可以利用object的wait和notify方法。

2.两个线程在执行各自的打印方法的时候,不应该被打断,所以把打印方法设置成同步的方法。

3.两个线程何时停止打印?当两个线程的打印方法都执行了26次的时候。

 

实现:

1.printstr类,用来完成打印,拥有printnumber和printletter两个同步方法

 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 }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网