现在的位置: 首页 > 综合 > 正文

java 一个线程执行加,一个线程执行减

2013年10月19日 ⁄ 综合 ⁄ 共 594字 ⁄ 字号 评论关闭
package com.base;
/**
 * 
 * @author zzh
 * 一个线程执行加,一个线程执行减
 */
class MyThreadA implements Runnable{
     private boolean flag=true;
     public void run(){
         int i=0;
         while(this.flag){
             while(true){
                 System.out.println(Thread.currentThread().getName()+"运行,i="+(i++));
             }
         }
     }
 }
     class MyThreadB implements Runnable{
         private boolean flag=true;
         public void run(){
             int i=0;
             while(this.flag){
                 while(true){
                     System.out.println(Thread.currentThread().getName()+"运行,i="+(i--));
                 }
             }
         }
     }
         
 public class Mythread {
     public static void main(String[] args) {
         MyThreadA A=new MyThreadA();
         MyThreadB B=new MyThreadB();
         Thread A1=new Thread(A,"线程");
         Thread B1=new Thread(B,"xc");
         A1.start();
         B1.start();
 
     }
 
 }

抱歉!评论已关闭.