A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郑飞 高级黑马   /  2014-10-20 16:22  /  967 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class ShareArrThread {
  2.    /*
  3.    * 声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),
  4.    * 给数组中添加数据,每一个线程为数组添加3个数据即可。
  5.    *
  6.    * 刚开始看视频,自己跟这写,感觉很乱很麻烦,现在整理下代码,供参考;
  7.    * 主要地方做了封装和注释说明,应该能看的比较清楚;
  8.    * 为主要代码看起来方便,异常在run中简单处理;
  9.    */
  10.         public static void main(String[] args)
  11.         {
  12.                 //new两个线程,每个线程循环3次(不是这题要点);
  13.                 final Arr arr = Arr.getArr();
  14.                 new Thread(
  15.                                 new Runnable()
  16.                                 {
  17.                                         public void run()
  18.                                         {
  19.                                                 for(int i = 0;i<3;i++)
  20.                                                         try {arr.addA();} catch (InterruptedException e) {}
  21.                                         }
  22.                                 }
  23.                         ).start();
  24.                
  25.                 new Thread(
  26.                                 new Runnable()
  27.                                 {
  28.                                         public void run()
  29.                                         {
  30.                                                 for(int i = 0;i<3;i++)
  31.                                                         try {arr.addB();} catch (InterruptedException e) {}
  32.                                         }
  33.                                 }
  34.                         ).start();
  35.         }
  36. }
  37. class Arr
  38. {
  39.         //以下三句为单例设计
  40.         private Arr(){}
  41.         private static Arr a = null;
  42.         public static Arr getArr()
  43.         {
  44.                 if(a==null)
  45.                         synchronized(Arr.class)
  46.                         {
  47.                                 if(a==null)
  48.                                         a = new Arr();
  49.                         }
  50.                 return a;
  51.         }
  52.         //下面两句数组相关初始化
  53.         public int[] arr = new int[6];
  54.         private int i = -1 ;
  55.        
  56.         //以下两个方法控制两个线程间隔调用
  57.         boolean flag = true;
  58.         public synchronized void addA() throws InterruptedException
  59.         {
  60.                 while(flag)
  61.                         wait();
  62.                 add();
  63.         }
  64.         public synchronized void addB() throws InterruptedException
  65.         {
  66.                 while(!flag)
  67.                         wait();
  68.                 add();
  69.         }
  70.         //添加元素核心方法
  71.         public void add() throws InterruptedException
  72.         {
  73.                 Thread.sleep((int)(Math.random()*1000));//每次添加间隔随机时间
  74.                 arr[++i]=(int)(Math.random()*100);//添加随机数到数组
  75.                 System.out.println(Thread.currentThread().getName()+"添加了元素"+arr[i]);
  76.                 flag = !flag;//倒置标记
  77.                 notify();
  78.         }
  79. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马