欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java多线程实现同时输出

程序员文章站 2024-03-08 23:06:46
一道经典的面试题目:两个线程,分别打印ab,其中线程a打印a,线程b打印b,各打印10次,使之出现ababababa.. 的效果 package com.sha...

一道经典的面试题目:两个线程,分别打印ab,其中线程a打印a,线程b打印b,各打印10次,使之出现ababababa.. 的效果

 package com.shangshe.path;
 
 public class threadab {
 
   /**
   * @param args
   */
   public static void main(string[] args) {
     
     final print business = new print();
     
     new thread(new runnable() {
       public void run() {
         for(int i=0;i<10;i++) {
           business.print_a();
         }
       }
     }).start();
     
     new thread(new runnable() {
       public void run() {
         for(int i=0;i<10;i++) {
           business.print_b();
         }
       }
     }).start();
     
   }
 }
 class print {
   
   private boolean flag = true;
   
   public synchronized void print_a () {
     while(!flag) {
       try {
         this.wait();
       } catch (interruptedexception e) {
         // todo auto-generated catch block
         e.printstacktrace();
       }
     }
     system.out.print("a");
     flag = false;
     this.notify();
   }
   
   public synchronized void print_b () {
     while(flag) {
       try {
         this.wait();
       } catch (interruptedexception e) {
         // todo auto-generated catch block
         e.printstacktrace();
       }
     }
     system.out.print("b");
     flag = true;
     this.notify();
   }
 }

由上面的例子我们可以设计出3个线程乃至于n个线程的程序,下面给出的例子是3个线程,分别打印a,b,c 10次,使之出现abcabc.. 的效果

public class threadabc {

  /**
   * @param args
   */
  public static void main(string[] args) {
    
    final print business = new print();
    
    new thread(new runnable() {
      public void run() {
        for(int i=0;i<100;i++) {
          business.print_a();
        }
      }
    }).start();
    
    new thread(new runnable() {
      public void run() {
        for(int i=0;i<100;i++) {
          business.print_b();
        }
      }
    }).start();
    
    new thread(new runnable() {
      public void run() {
        for(int i=0;i<100;i++) {
          business.print_c();
        }
      }
    }).start();
    
  }
}
class print {
  
  private boolean should_a = true;
  private boolean should_b = false;
  private boolean should_c = false;
  
  public synchronized void print_a () {
    while(should_b || should_c) {
      try {
        this.wait();
      } catch (interruptedexception e) {
        // todo auto-generated catch block
        e.printstacktrace();
      }
    }
    system.out.print("a");
    should_a = false;
    should_b = true;
    should_c = false;
    this.notifyall();
  }
  
  public synchronized void print_b () {
    while(should_a || should_c) {
      try {
        this.wait();
      } catch (interruptedexception e) {
        // todo auto-generated catch block
        e.printstacktrace();
      }
    }
    system.out.print("b");
    should_a = false;
    should_b = false;
    should_c = true;
    this.notifyall();
  }
  
  public synchronized void print_c () {
    while(should_a || should_b) {
      try {
        this.wait();
      } catch (interruptedexception e) {
        // todo auto-generated catch block
        e.printstacktrace();
      }
    }
    system.out.print("c");
    should_a = true;
    should_b = false;
    should_c = false;
    this.notifyall();
  }
}

再一次证明了软件工程的重要性了;在多线程程序中,应该说在程序中,我们应该把那些业务逻辑代码放到同一个类中,使之高内聚,低耦合