【java|java并发编程(顺序输出A、B、C循环10次)】要求:3个线程,分别输出A、B、C。循环10次,给出三种方法,code如下
一、wait、notify
public class PrintABC {
static int state = 0;
private static Object o = new Object();
public static void main(String[] args) {
ExecutorService ser = Executors.newCachedThreadPool();
ser.submit(new ThreadA());
ser.submit(new ThreadB());
ser.submit(new ThreadC());
ser.shutdown();
} public static class ThreadA implements Runnable {@Override
public void run() {for(int i=0;
i<10;
i++){
synchronized (o) {
while(state%3!=0){
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A");
state++;
o.notifyAll();
}}
} } public static class ThreadB implements Runnable {
@Override
public void run() {
for(int i=0;
i<10;
i++){
synchronized (o) {
while(state%3!=1){
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B");
state++;
o.notifyAll();
}
}
} } public static class ThreadC implements Runnable {
@Override
public void run() {
for(int i=0;
i<10;
i++){
synchronized (o) {
while(state%3!=2){
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("C");
state++;
o.notifyAll();
}
}
} }
}
二、lock、unlock
public class PrintABC2 {
static int state = 0;
private static Lock lock = new ReentrantLock();
public static void main(String[] args) {
ExecutorService ser = Executors.newCachedThreadPool();
ser.submit(new ThreadA());
ser.submit(new ThreadB());
ser.submit(new ThreadC());
ser.shutdown();
} public static class ThreadA implements Runnable {@Override
public void run() {for(int i=0;
i<10;
){
lock.lock();
if(state%3==0){
System.out.println("A");
state++;
i++;
}
lock.unlock();
}}
} public static class ThreadB implements Runnable {
@Override
public void run() {
for(int i=0;
i<10;
){
lock.lock();
if(state%3==1){
System.out.println("B");
state++;
i++;
}
lock.unlock();
}
} } public static class ThreadC implements Runnable {
@Override
public void run() {
for(int i=0;
i<10;
){
lock.lock();
if(state%3==2){
System.out.println("C");
state++;
i++;
}
lock.unlock();
}
} }
}
三、信号量
public class PrintABC3 {
static int state = 0;
private static Semaphore A = new Semaphore(1);
private static Semaphore B = new Semaphore(1);
private static Semaphore C = new Semaphore(1);
public static void main(String[] args) {
ExecutorService ser = Executors.newCachedThreadPool();
try {
B.acquire();
C.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ser.submit(new ThreadA());
ser.submit(new ThreadB());
ser.submit(new ThreadC());
ser.shutdown();
}
static class ThreadA extends Thread {@Override
public void run() {
try {
for (int i = 0;
i < 10;
i++) {
A.acquire();
System.out.print("A");
sleep(2000);
B.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}}static class ThreadB extends Thread {@Override
public void run() {
try {
for (int i = 0;
i < 10;
i++) {
B.acquire();
System.out.print("B");
C.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}}static class ThreadC extends Thread {
@Override
public void run() {
try {
for (int i = 0;
i < 10;
i++) {
C.acquire();
System.out.print("C");
A.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}}}
结果如下:
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)