编写代码,使用3个线程,1个线程打印X,一个线程打印Y,一个线程打印Z,同时执行连续打印10次"XYZ"
import java.util.concurrent.Semaphore; public class PrintXYZ { private static Semaphore X=new Semaphore(1); private static Semaphore Y=new Semaphore(1); private static Semaphore Z=new Semaphore(1); static class ThreadX extends Thread{ public void run(){ try{ for (int i=0;i<10;i++){ X.acquire(); System.out.print("X"); Y.release(); } }catch(Exception e){ e.printStackTrace(); } } } static class ThreadY extends Thread{ public void run(){ try{ for (int i=0;i<10;i++){ Y.acquire(); System.out.print("Y"); Z.release(); } }catch(Exception e){ e.printStackTrace(); } } } static class ThreadZ extends Thread{ public void run(){ try{ for (int i=0;i<10;i++){ Z.acquire(); System.out.print("Z"+"\t"); X.release(); } }catch(Exception e){ e.printStackTrace(); } } } public static void main(String [] args) throws Exception{ //最开始X可以获取 保证X可以先执行 Y.acquire(); Z.acquire(); new ThreadX().start(); new ThreadY().start(); new ThreadZ().start(); } }
free online casino slots free casino slots with bonuses free casino https://onlinecasinogameslots21.com/
Lycrad [url=https://oscialipop.com]Cialis[/url] This will include a pelvic exam. Dqbgyz cialis for sale online Bolbpd https://oscialipop.com – Cialis Bgymng