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

1 class MyThreadA implements Runnable{
         
   2     private boolean flag=true;  3     public void run(){
         
   4         int i=0;  5         while(this.flag){
         
   6             while(true){
         
   7                 System.out.println(Thread.currentThread().getName()+"运行,i="+(i++));  8             }  9         } 10     } 11 } 12     class MyThreadB implements Runnable{
         
  13         private boolean flag=true; 14         public void run(){
         
  15             int i=0; 16             while(this.flag){
         
  17                 while(true){
         
  18                     System.out.println(Thread.currentThread().getName()+"运行,i="+(i--)); 19                 } 20             } 21         } 22     } 23         24 public class StopDemo {
         
  25     public static void main(String[] args) {
         
  26         MyThreadA A=new MyThreadA(); 27         MyThreadB B=new MyThreadB(); 28         Thread A1=new Thread(A,"线程"); 29         Thread B1=new Thread(B,"xc"); 30         A1.start(); 31         B1.start(); 32 33     } 34 35 }
1 class MyThreadA implements Runnable{ 2 private boolean flag=true; 3 public void run(){ 4 int i=0; 5 while(this.flag){ 6 while(true){ 7 System.out.println(Thread.currentThread().getName()+"运行,i="+(i++)); 8 } 9 } 10 } 11 } 12 class MyThreadB implements Runnable{ 13 private boolean flag=true; 14 public void run(){ 15 int i=0; 16 while(this.flag){ 17 while(true){ 18 System.out.println(Thread.currentThread().getName()+"运行,i="+(i--)); 19 } 20 } 21 } 22 } 23 24 public class StopDemo { 25 public static void main(String[] args) { 26 MyThreadA A=new MyThreadA(); 27 MyThreadB B=new MyThreadB(); 28 Thread A1=new Thread(A,"线程"); 29 Thread B1=new Thread(B,"xc"); 30 A1.start(); 31 B1.start(); 32 33 } 34 35 }
经验分享 程序员 微信小程序 职场和发展