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

JAVA 多线程一

2012-10-07 
JAVA 多线程1package com.testpublic class ThreadTest{/** * @param args */public static void main(St

JAVA 多线程1

package com.test;public class ThreadTest{/** * @param args */public static void main(String[] args){final Utils ts = new Utils();new Thread(){@Overridepublic void run(){for (int i = 0; i < 30; i++){ts.sub(i);}}}.start();for (int i = 0; i < 30; i++){ts.main(i);}}}class Utils{private boolean flag = true;public synchronized void main(int j){if(!flag){try{this.wait();}catch (InterruptedException e){e.printStackTrace();}}for (int i = 1; i <= 5; i++){System.out.println(j+":this's main"+i);}this.flag = false;this.notify();}public synchronized void sub(int j){if(flag){try{this.wait();}catch (InterruptedException e){e.printStackTrace();}}for (int i = 1; i <= 10; i++){System.out.println(j+":this's sub"+i);}this.flag = true;this.notifyAll();}}

热点排行