首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-11-08 
启动三个线程循环打印ABC10次public class Test implements Runnable {private static char[] chars new

启动三个线程循环打印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++;}}}}

热点排行