本文主要是介绍App11,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 项目名称: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();}}
}
这篇关于App11的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!