Java多线程实战之交叉打印的两种方法
时间:2021-04-07 10:14:55|栏目: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) {
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()方法
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(MyService 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();
}
}
}
总结
上一篇:SpringBoot @RequestParam、@PathVaribale、@RequestBody实战案例
栏 目:JAVA代码
下一篇:解决Java 部署Tomcat时使用jni和jna调用DLL文件的问题
本文标题:Java多线程实战之交叉打印的两种方法
本文地址:http://www.codeinn.net/misctech/96407.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




