App11

来源:互联网 发布:淘宝入门教程 编辑:程序博客网 时间:2024/09/21 08:52
/** * 项目名称:App11_多线程0929  * 文件名:Main.java * 版本信息: * 日期:2014年9月29日 * Copyright asiainf-linkage Corporation 2014 * 版权所有 * *//** * Main *  * @author:shaojh@asiainfo.com * @2014年9月29日 下午12:44:22 * @version 1.0 */public class Main {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubnew Main().init();}public void init() {final Business business = new Business();new Thread(new Runnable() {public void run() {for (int i = 0; i < 50; i++) {business.SubThread(i);}}}).start();//for (int i = 0; i < 50; i++) {//business.SubThread(i);//}for (int i = 0; i < 50; i++) {business.MainThread(i);}}private class Business {boolean bShouldSub = true;// 这里相当于定义了控制该谁执行的一个信号灯public synchronized void MainThread(int i) {if (bShouldSub)try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}for (int j = 0; j < 100; j++) {System.out.println(Thread.currentThread().getName() + ":i=" + i+ ",j=" + j);}bShouldSub = true;this.notify();}public synchronized void SubThread(int i) {if (!bShouldSub)try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}for (int j = 0; j < 10; j++) {System.out.println(Thread.currentThread().getName() + ":i=" + i+ ",j=" + j);}bShouldSub = false;this.notify();}}}

0 0