附加题---需要使用synchronized和lock两种方式来完成
1:启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到打印到75. 程序的输出结果应该为:
线程1: 1
线程1: 2
线程1: 3
线程1: 4
线程1: 5
线程2: 6
线程2: 7
线程2: 8
线程2: 9
线程2: 10
...
线程3: 71
线程3: 72
线程3: 73
线程3: 74
线程3: 75
解题思路:先定义一个int数,默认为1,表示从1开始,接着定义一个标识,来控制锁对象 ,利用分离的思想创建三个方法。
synchronized方式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61public class PrintNum { public int num=1;//从1开始打印 public int isPrint=1;//标识,1,2,3中标识分别代表三种状态 //synchronized方式 //方法一 标识1 public synchronized void fun1() { while (isPrint!=1) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } printXunHuan(); isPrint=2; notifyAll(); } //方法二 标识2 public synchronized void fun2() { while (isPrint!=2) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } printXunHuan(); isPrint=3; notifyAll(); } //方法三 标识3 public synchronized void fun3() { while (isPrint!=3) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } printXunHuan(); isPrint=1; notifyAll(); } public void printXunHuan() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName()+":"+num++); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38import com.qf.bean.PrintNum; public class PrintNumTest { public static void main(String[] args) { // TODO Auto-generated method stub PrintNum printNum=new PrintNum(); //线程1 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (printNum.num<=65) { printNum.fun1(); } } }, "线程1").start(); //线程2 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (printNum.num<=70) { printNum.fun2(); } } }, "线程2").start(); //线程3 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (printNum.num<=75) { printNum.fun3(); } } }, "线程3").start(); } }
lock方式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class PrintNumLock { private int num=1;//从1开始 private int isPrint=1;//标识 1,2,3 public int getNum() { return num; } public void setNum(int num) { this.num = num; } public PrintNumLock() { super(); } final Lock lock=new ReentrantLock(); final Condition oneCondition=lock.newCondition(); final Condition twoCondition=lock.newCondition(); final Condition threeCondition=lock.newCondition(); //锁1 public void funOne() { lock.lock(); try { while (isPrint!=1) { try { oneCondition.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } printXunHuan(); isPrint=2;//切换标识2 唤醒锁2 twoCondition.signal(); } finally { // TODO: handle finally clause lock.unlock(); } } //锁2 public void funTwo() { lock.lock(); try { while (isPrint!=2) { try { twoCondition.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } printXunHuan(); isPrint=3;//切换标识3 唤醒锁3 threeCondition.signal(); } finally { // TODO: handle finally clause lock.unlock(); } } //锁3 public void funThree() { lock.lock(); try { while (isPrint!=3) { try { threeCondition.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } printXunHuan(); isPrint=1;//切换为标识1 唤醒锁1 oneCondition.signal(); } finally { // TODO: handle finally clause lock.unlock(); } } //控制循环5次 public void printXunHuan() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName()+":"+num++); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35import com.qf.bean.PrintNumLock; public class PrintNumLockTest { public static void main(String[] args) { // TODO Auto-generated method stub PrintNumLock printnum=new PrintNumLock(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (printnum.getNum()<=65) { printnum.funOne(); } } }, "线程1").start(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (printnum.getNum()<=70) { printnum.funTwo(); } } }, "线程2").start(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (printnum.getNum()<=75) { printnum.funThree(); } } }, "线程3").start(); } }
本题还有简单的解法 ,就是把三个方法合并为一个进行控制。大家有兴趣可以试试。。。。。
最后
以上就是雪白毛豆最近收集整理的关于面试题:启动3个线程打印递增的数字.........的全部内容,更多相关面试题:启动3个线程打印递增内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复