读书人

起步三个线程循环打印ABC10次

发布时间: 2012-11-08 08:48:11 作者: rapoo

启动三个线程循环打印ABC10次

public class Test implements Runnable {private static char[] chars = new char[] { 'A', 'B', 'C' };private static Integer index = 0; // 临界资源private int count = 10;public static void main(String[] args) {new Thread(new Test()).start();new Thread(new Test()).start();new Thread(new Test()).start();}public void run() {while (true) {synchronized (index) {if (index == 3*count){return;}System.out.println(chars[index % 3]);index++;}}}}

读书人网 >编程

热点推荐