要求效果:先打印5次“printa…”,再打印5次“printb…”,每次打印间隔1秒,重复循环20次
方式一:使用wait()和notifyall()方法
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
|
public class myservice {
private volatile boolean flag = false ;
public synchronized void printa() {
try {
while (flag) {
wait();
}
for ( int i = 0 ; i < 5 ; i++) {
system.out.println( "printa..." );
timeunit.seconds.sleep( 1 );
}
flag = true ;
notifyall();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
public synchronized void printb() {
try {
while (!flag) {
wait();
}
for ( int i = 0 ; i < 5 ; i++) {
system.out.println( "printb..." );
timeunit.seconds.sleep( 1 );
}
flag = false ;
notifyall();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
public class backupa implements runnable {
private myservice myservice;
public backupa(myservice myservice) {
super ();
this .myservice = myservice;
}
@override
public void run() {
myservice.printa();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
public class backupb implements runnable {
private myservice myservice;
public backupb(myservice myservice) {
super ();
this .myservice = myservice;
}
@override
public void run() {
myservice.printb();
}
}
|
1
2
3
4
5
6
7
8
9
|
public class run {
public static void main(string[] args) {
myservice myservice = new myservice();
for ( int i = 0 ; i < 20 ; i++) {
new thread( new backupa(myservice)).start();
new thread( new backupb(myservice)).start();
}
}
}
|
方式二:使用await()和signalall()方法
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
|
public class myservice {
private lock lock = new reentrantlock();
private condition condition = lock.newcondition();
private boolean flag = false ;
public void printa() {
try {
lock.lock();
while (flag) {
condition.await();
}
for ( int i = 0 ; i < 5 ; i++) {
system.out.println( "printa..." );
timeunit.seconds.sleep( 1 );
}
flag = true ;
condition.signalall();
} catch (interruptedexception e) {
e.printstacktrace();
} finally {
lock.unlock();
}
}
public void printb() {
try {
lock.lock();
while (!flag) {
condition.await();
}
for ( int i = 0 ; i < 5 ; i++) {
system.out.println( "printb..." );
timeunit.seconds.sleep( 1 );
}
flag = false ;
condition.signalall();
} catch (interruptedexception e) {
e.printstacktrace();
} finally {
lock.unlock();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
public class threada implements runnable {
private myservice myservice;
public threada(myservice myservice) {
super ();
this .myservice = myservice;
}
@override
public void run() {
myservice.printa();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
public class threadb implements runnable {
private myservice myservice;
public threadb(myservice myservice) {
super ();
this .myservice = myservice;
}
@override
public void run() {
myservice.printb();
}
}
|
1
2
3
4
5
6
7
8
9
|
public class run {
public static void main(string[] args) {
myservice myservice = new myservice();
for ( int i = 0 ; i < 20 ; i++) {
new thread( new threada(myservice)).start();
new thread( new threadb(myservice)).start();
}
}
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/qq_40378034/article/details/86766001