JAVA 多线程轮流打印ABC

来源:互联网 发布:qq mac版本能远程协助 编辑:程序博客网 时间:2024/06/07 23:29

采用Thread+Semaphore实现,思路很简单

import java.io.IOException;import java.util.concurrent.Semaphore;public class PrintABC {    public static int MAX_TIME = 10;    public static class PrintThread extends Thread {        private String printChar;        private Semaphore curSemaphore, nextSemaphore;        public PrintThread(String printChar, Semaphore curSemaphore,                Semaphore nextSemaphore) {            this.printChar = printChar;            this.curSemaphore = curSemaphore;            this.nextSemaphore = nextSemaphore;        }        public void run() {            for (int i = 0; i < MAX_TIME; ++i) {                try {                    curSemaphore.acquire();  /* 请求打印需要的信号量,资源数-1,即down操作  */                    System.out.println(printChar);                    nextSemaphore.release(); /* 释放下一个线程打印需要的信号量,资源数+1,即up操作  */                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }    public static void main(String[] args) throws IOException {        Semaphore semaphoreA = new Semaphore(1); /* 只有A信号量的初始资源数为1,保证从A开始打印  */        Semaphore semaphoreB = new Semaphore(0);        Semaphore semaphoreC = new Semaphore(0);        new PrintThread("A", semaphoreA, semaphoreB).start();        new PrintThread("B", semaphoreB, semaphoreC).start();        new PrintThread("C", semaphoreC, semaphoreA).start();    }}


0 0
原创粉丝点击