Java多线程实战之交叉打印的两种方法

网友投稿 295 2023-01-13


Java多线程实战之交叉打印的两种方法

要求效果:先打印5次“printA…”,再打印5次“printB…”,每次打印间隔1秒,重复循环20次

方式一:使用wait()和notifyAll()方法

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();

}

}

}

public class BackupA implements Runnable {

private MyService myService;

public BackupA(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printA();

}

}

public class BackupB implements Runnable {

private MyService myService;

public BackupB(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printB();

}

}

public class Run {

public static void main(String[] args) {

MySJUIyZzervice myService = new MyService();

for (int i = 0; i < 20; i++) {

new Thread(new BackupA(myService)).start();

new Thread(new BackupB(myService)).start();

}

}

}

方式二:使用await()和signalAll()方法

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();

}

}

}

public class ThreadA implements Runnable {

private MyService myService;

public ThreadA(MySerJUIyZzvice myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printA();

}

}

public class ThreadB implements Runnable {

private MyService myService;

public ThreadB(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printB();

}

}

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();

}

}

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:包含python接口测试用例的词条
下一篇:Spring Boot集成Thymeleaf模板引擎的完整步骤
相关文章

 发表评论

暂时没有评论,来抢沙发吧~