多线程练习--顺序打印ABC十次

这是迅雷的一道面试题,顺序打印ABC十次。


【多线程练习--顺序打印ABC十次】

public class TenABC { public static void main(String[] args) { final BlockingQueue queue = new ArrayBlockingQueue(30); for (int i = 0; i < 10; i++) { try { queue.put("A"); queue.put("B"); queue.put("C"); } catch (InterruptedException e) { e.printStackTrace(); } }new Thread(new Runnable() { @Override public void run() { boolean flag =true; while(flag){ if(queue.size()!=0){ try { System.out.print(queue.take()); } catch (InterruptedException e) { e.printStackTrace(); } } else{ flag=false; } }} }).start(); } }


ABCABCABCABCABCABCABCABCABCABC

    推荐阅读