java线程输出线程开始:1-2-a## 3-4-b## 5-6-c## 7-8-d## 9-0-e## 线程结束

avatar 2017年05月29日13:59:56 1 1920 views
博主分享免费Java教学视频,B站账号:Java刘哥
题目
借助同步机制、sleep()方法、join()方法,实现动画显示: 甲线程输出:1、3、5、7、9 乙线程输出:2、4、6、8、0 丙线程数出:a、b、c、d、e main线程输出:线程开始、线程结束 最终输出结果为(注:这是唯一可能的结果) 线程开始:1-2-a## 3-4-b## 5-6-c## 7-8-d## 9-0-e## 线程结束 要求:每隔一秒输出一个字符。(借助sleep)
分析 可以参考Java线程生产者和消费者实例 代码如下
  1. package com.liuyanzhao;
  2. /*
  3.  *
  4.  * @author WellsLiu
  5.  *
  6.  */
  7. class Resource {
  8.     int flag=1;
  9. }
  10. class Thread1 extends Thread {
  11.     Resource res;
  12.     public Thread1(Resource res) {
  13.         this.res = res;
  14.     }
  15.     int i=0;
  16.     char []c1 = {'1','3','5','7','9'};
  17.     public  void run() {
  18.         synchronized (res) {
  19.             while(i<5) {
  20.                 while(res.flag!=1) {
  21.                     try {
  22.                         res.wait();//t1
  23.                     } catch (InterruptedException e) {
  24.                         e.printStackTrace();
  25.                     }
  26.                 }
  27.                 System.out.print(c1[i]);
  28.                 try {
  29.                     sleep(1000);
  30.                 } catch (InterruptedException e) {
  31.                     e.printStackTrace();
  32.                 }
  33.                 System.out.print("-");
  34.                 try {
  35.                     sleep(1000);
  36.                 } catch (InterruptedException e) {
  37.                     e.printStackTrace();
  38.                 }
  39.                 i++;
  40.                 res.notifyAll();
  41.                 res.flag=2;
  42.             }
  43.         }
  44.     }
  45. }
  46. class Thread2 extends Thread {
  47.     Resource res;
  48.     public Thread2(Resource res) {
  49.         this.res = res;
  50.     }
  51.     int i=0;
  52.     char []c1 = {'2','4','6','8','0'};
  53.     public  void run() {
  54.         synchronized (res) {
  55.             while(i<5) {
  56.                 while(res.flag!=2) {
  57.                     try {
  58.                         res.wait();
  59.                     } catch (InterruptedException e) {
  60.                         e.printStackTrace();
  61.                     }
  62.                 }
  63.                 System.out.print(c1[i]);
  64.                 try {
  65.                     sleep(1000);
  66.                 } catch (InterruptedException e) {
  67.                     e.printStackTrace();
  68.                 }
  69.                 System.out.print("-");
  70.                 try {
  71.                     sleep(1000);
  72.                 } catch (InterruptedException e) {
  73.                     // TODO Auto-generated catch block
  74.                     e.printStackTrace();
  75.                 }
  76.                 i++;
  77.                 res.notifyAll();
  78.                 res.flag=3;
  79.             }
  80.         }
  81.     }
  82. }
  83. class Thread3 extends Thread {
  84.     Resource res;
  85.     public Thread3(Resource res) {
  86.         this.res = res;
  87.     }
  88.     int i=0;
  89.     char []c1 = {'a','b','c','d','e'};
  90.     public  void run() {
  91.         synchronized (res) {
  92.             while(i<5) {
  93.                 while(res.flag!=3) {
  94.                     try {
  95.                         res.wait();
  96.                     } catch (InterruptedException e) {
  97.                         e.printStackTrace();
  98.                     }
  99.                 }
  100.                 System.out.print(c1[i]);
  101.                 try {
  102.                     sleep(1000);
  103.                 } catch (InterruptedException e) {
  104.                     e.printStackTrace();
  105.                 }
  106.                 System.out.print("#");
  107.                 try {
  108.                     sleep(1000);
  109.                 } catch (InterruptedException e) {
  110.                     e.printStackTrace();
  111.                 }
  112.                 System.out.print("#");
  113.                 try {
  114.                     sleep(1000);
  115.                 } catch (InterruptedException e) {
  116.                     e.printStackTrace();
  117.                 }
  118.                 System.out.print(" ");
  119.                 i++;
  120.                 res.notifyAll();
  121.                 res.flag=1;
  122.             }
  123.         }
  124.     }
  125. }
  126. public class Test1 {
  127.     public static void main(String[] args) throws InterruptedException {
  128.         System.out.printf("线程开始: ");
  129.         Resource res = new Resource();
  130.         Thread1 t1 = new Thread1(res);
  131.         Thread2 t2 = new Thread2(res);
  132.         Thread3 t3 = new Thread3(res);
  133.         t1.start();
  134.         t2.start();
  135.         t3.start();
  136.         t1.join();
  137.         t3.join();
  138.         t2.join();
  139.         System.out.print("线程结束");
  140.     }
  141. }
运行结果如下   本文链接:https://liuyanzhao.com/4481.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0