• 技术文章 >Java >java教程

    Java多线程实现同时输出

    高洛峰高洛峰2017-01-05 16:58:34原创750
    一道经典的面试题目:两个线程,分别打印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();
      }
    }

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


    更多Java多线程实现同时输出相关文章请关注PHP中文网!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:Java多线程
    上一篇:Java单利模式与多线程总结归纳 下一篇:深入理解JAVA多线程之线程间的通信方式
    大前端线上培训班

    相关文章推荐

    • 理解java8中java.util.function.*pojo反射新方法(附代码)• 浅析安卓app和微信授权登录及分享完整对接(代码分享)• 教你一招搞定时序数据库在Spring Boot中的使用• 一招教你使用java快速创建Map(代码分享)• PlayFramework 完整实现一个APP(十一)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网