java并发编程:顺序输出A、B、C循环10次
来源:互联网 发布:java 就业班 编辑:程序博客网 时间:2024/05/16 04:49
要求: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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 blocke.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(); } } } }
结果如下:
ABCABCABCABCABCABCABCABCABCABC
0 0
- java并发编程:顺序输出A、B、C循环10次
- JAVA A B C 循环打印10次实现
- 启动三个线程A,B,C,打印10次 按照ABC的顺序输出
- 进程1,2,3分别打印A,B,C,输出10次,按ABC顺序
- 写一个程序三个线程分别输出A,B,C, 顺序输出ABC十次,用java实现
- 多线程(至少三个线程)分别打印A、B、C,要求按ABC的顺序循环打印10次。
- java实现【有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABC.】
- 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC
- 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC
- 有三个线程ID分别是A、B、C,请有多线编程 实现 在屏幕上循环打印10次ABCABC......
- 有三个线程A、B、C(线程名称或id),循环打印10次ABCABB
- 有三个线程A,B,C 循环10次打印出三个线程的名字ABCABCABCABCABCABCABCABCABCABC
- 有三个线程A、B、C(线程名称或id),循环打印10次ABCABB…
- ReentrantLock机制实现:有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC
- java 输入3个数a,b,c,按大小顺序输出。
- java多线程循环打印A,B,C
- 输入3个数a,b,c,按大小顺序输出。
- 输入三个数a,b,c,要求从小到大顺序输出
- 视图弹出后放大又缩小的动画实现、类似于alertView效果
- Lua 性能优化
- iOS中-Block使用
- 迷之RxJava —— 线程切换
- 怎么防止服务器的任务因断网中断(linux screen 命令详解)
- java并发编程:顺序输出A、B、C循环10次
- .NET MVC实现多图片上传并附带参数(ajaxfileupload)
- NYOJ 164 Game of Connections
- Dubbo消费者配置
- ls
- Nhiernate 学习(一),环境准备
- AndroidStudio下SVN使用介绍
- jQuery 自定义事件
- 可扩散列