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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 在学野马 中级黑马   /  2014-10-4 15:08  /  1318 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 在学野马 于 2014-10-5 07:36 编辑

public class xiancheng {
         public static int[]arr=new int[6];
        public  int dex=0;
        public static void main(String[] args) throws Exception {

        new Thread(new thread()).start();
        new Thread(new thread()).start();
        Thread.sleep(2200);
        for(int a:arr){
                System.out.println(a);
        }
        }

}
class thread implements Runnable{
         int[]arr=new int[6];
         int dex=0;
        @Override
        public void run() {
                try {
                        Thread.sleep(3000);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                for(int i=0;i<3;i++){
                        int num=(int) (Math.random()*10+1);
                        synchronized(xiancheng.class){
                        arr[dex]=num;
                        System.out.println(arr[dex]);
                        dex++;
                        }
                }
        }
        
}
没有达到效果 这是哪的问题

评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

8 个回复

倒序浏览
没看出来你想要什么效果,三个线程各自操作各自的数组,不涉及任何对共享资源的操作,三个线程还有一段时间在同时睡觉。两个新线程循环体里面数组角标变化明明和循环码子完全相同,还是弄成了两个变量。主线程数组默认初始化,所以打印六个零,两个新线程各自打印数组中前三个元素,这样的输出不是理所当然么,根本看不出你期望的效果是什么。

评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

回复 使用道具 举报
水竹 发表于 2014-10-4 15:38
没看出来你想要什么效果,三个线程各自操作各自的数组,不涉及任何对共享资源的操作,三个线程还有一段时间 ...

package lianxi;

public class xiancheng implements Runnable{
        private static int[] arr=new int[6];
        int number=0;
        public static void main(String[] args) throws Exception{
                new Thread(new xiancheng()).start();
                new Thread(new xiancheng()).start();
                Thread.sleep(2000);
                for(int i:arr){
                        System.out.println(i);
                }
               
        }

        @Override
        public void run() {

                try {
                        Thread.sleep(500);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                for(int i=0;i<3;i++){
                        int sum=(int) (Math.random()*10+1);
                        synchronized(xiancheng.class){
                                arr[number]=sum;
                                System.out.println(arr[number]);
                                number++;
                        }
                }
        }
}
这样呢 怎么 也没有共享
回复 使用道具 举报
在学野马 发表于 2014-10-4 16:01
package lianxi;

public class xiancheng implements Runnable{

number并不是static的,所以两个新线程中都各有一个number,所以其实他们操作的都是数组中的前三个元素,先执行的线程写入的内容被后执行的线程重新赋值了。要想把数组写满,把number修饰为static的即可。
回复 使用道具 举报
水竹 发表于 2014-10-4 16:39
number并不是static的,所以两个新线程中都各有一个number,所以其实他们操作的都是数组中的前三个元素, ...

当我把睡眠时间改了以后 为什么会出现好几个0的现象
回复 使用道具 举报
这样吧, 楼主先看一遍多线程部分的银行存钱的例子,思路是一样的。
以下是我的代码,参考下吧~ 希望能帮到你。

  1. package com.JerryJava;

  2. import java.util.Random;

  3. public class interview_04_2 {

  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                 // TODO Auto-generated method stub
  9.                 myArrayAdd ma = new myArrayAdd();
  10. //                ma.run();
  11.                 new Thread(ma).start();
  12.                 new Thread(ma).start();
  13.                 new Thread(ma).start();
  14.                 new Thread(ma).start();
  15.         }

  16. }
  17. class myArray
  18. {
  19.         private int[] arr = new int[15];
  20.         private int pos = 0 ;
  21.         public synchronized void add(int num)
  22.         {
  23.                 int time = new Random().nextInt(10)+1;
  24.                 try {
  25.                         Thread.sleep(time*200);
  26.                 } catch (InterruptedException e) {
  27.                         // TODO Auto-generated catch block
  28.                         e.printStackTrace();
  29.                 }
  30.                 arr[pos]= num;
  31.                
  32.                 //System.out.println("arr::"+pos+"__"+arr[2]);
  33.                 System.out.println("arr"+pos+"=::"+arr[pos]);
  34.                 pos++;
  35.         }
  36. }
  37. class myArrayAdd implements Runnable
  38. {
  39.         private myArray ma = new myArray();
  40.         public void run(){
  41.                 for(int i=1 ; i<=3 ; i++)
  42.                 {
  43.                         int num = new Random().nextInt(20);
  44.                         //System.out.println(num);
  45.                         ma.add(num);
  46.                 }
  47.                
  48.                
  49.         }
  50. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

回复 使用道具 举报
水竹 中级黑马 2014-10-4 17:39:19
7#
在学野马 发表于 2014-10-4 16:49
当我把睡眠时间改了以后 为什么会出现好几个0的现象

是说又改了还是说上面的?
如果是number加上static后又改了睡眠时间的话,就要看主线程打印的时候其余两个线程是否已经执行完毕,若没有则会输出0.
如果说的上面的,是因为两个线程修改的都是数组前三个元素的值。
回复 使用道具 举报
JerryJava 发表于 2014-10-4 16:56
这样吧, 楼主先看一遍多线程部分的银行存钱的例子,思路是一样的。
以下是我的代码,参考下吧~ 希望能帮 ...

这个不算是共享吧
回复 使用道具 举报

我的思路:
1.将数组【15】,操作数组的指针 pos ,和向数组中添加元素的方法add 封装成一个类myArray.
2.建立一个Runnable的实现类myArrayAdd, 此类中初始就有一个myArray资源,run方法中分3次调用add方法,即向数组中存随机数。
3.主函数中建立一个myArrayAdd对象ma(只有一个),建立4个线程操作,注意由于只有一个对象,所以操作的是同一个数组。只是调用了4次run(),即向数组中add了12次随机数。

希望有用。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马