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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小十 中级黑马   /  2015-3-15 11:45  /  588 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Random;

  2. public class RunDeMo extends Thread {

  3.         public static void main(String[] args) throws Exception {
  4.                 // share用于保存2个线程添加的数据
  5.                 String[] share = new String[6];
  6.                 Thread t1 = new Thread(new AddData(share), "线程A");
  7.                 Thread t2 = new Thread(new AddData(share), "线程B");
  8.                 t1.start();
  9.                 t2.start();

  10.                 // 当2个线程执行完,才执行下面的main线程程序
  11.                 t2.join();
  12.                 t1.join();

  13.                 // 遍历share保存的数据
  14.                 for (int i = 0; i < share.length; i++) {
  15.                         System.out.println(Thread.currentThread().getName() + "--"
  16.                                         + share[i]);

  17.                 }
  18.         }

  19. }

  20. class AddData implements Runnable {

  21.         private String[] share;

  22.         public AddData(String[] share) {
  23.                 this.share = share;

  24.         }

  25.         @Override
  26.         public void run() {
  27.                 String[] data = { "第一个数据", "第二个数据", "第三个数据" };
  28.                 int index = 0;
  29.                 synchronized (share) {
  30.                         for (int i = 0; i < data.length; i++) {
  31.                                 try {
  32.                                         Thread.sleep((new Random().nextInt(10) + 1) * 10);
  33.                                 } catch (InterruptedException e) {
  34.                                         e.printStackTrace();
  35.                                 }
  36.                                 share[index++] = Thread.currentThread().getName() + ":"
  37.                                                 + data[i];
  38.                         }
  39.                 }
  40.         }
  41. }
复制代码

定义两个线程同时运行三次代码。
为什么只能读出来B的值A去哪了?
该线程怎么修改?

2 个回复

倒序浏览
本帖最后由 Shey 于 2015-3-15 12:28 编辑

1.synchronized是多个线程共同访问一个“对象”的某个数据是所起到的保护限制,你创建了两个AddData对象,那么对于两个线程所执行的对象是不同的,线程拥有资源也就不同(也就是属性不共用)
2.每个线程中对象的index在方法run内部,属于局部变量,每个线程start都会执行run方法,也就是都会临时创建一个index=0,这样对于数组share的0,1,2位置重复赋值2次,修改代码如下:

  1. package temp;

  2. import java.util.Random;

  3. public class Demo2 extends Thread {

  4.         public static void main(String[] args) throws Exception {
  5.                 // share用于保存2个线程添加的数据
  6.                 String[] share = new String[6];
  7.                 AddData addData = new AddData(share);
  8.                 Thread t1 = new Thread(addData, "线程A");
  9.                 Thread t2 = new Thread(addData, "线程B");
  10.                 t1.start();
  11.                 t2.start();

  12.                 // 当2个线程执行完,才执行下面的main线程程序               
  13.                 t1.join();
  14.                 t2.join();

  15.                 // 遍历share保存的数据
  16.                 for (int i = 0; i < share.length; i++) {
  17.                         System.out.println(Thread.currentThread().getName() + "--"
  18.                                         + share[i]);

  19.                 }
  20.         }

  21. }

  22. class AddData implements Runnable {

  23.         private String[] share;

  24.         public AddData(String[] share) {
  25.                 this.share = share;

  26.         }
  27.         int index = 0;
  28.         @Override
  29.         public void run() {
  30.                 String[] data = { "第一个数据", "第二个数据", "第三个数据" };
  31.                
  32.                 synchronized (share) {
  33.                         for (int i = 0; i < data.length; i++) {
  34.                                 try {
  35.                                         Thread.sleep((new Random().nextInt(10) + 1) * 10);
  36.                                 } catch (InterruptedException e) {
  37.                                         e.printStackTrace();
  38.                                 }
  39.                                 share[index++] = Thread.currentThread().getName() + ":"
  40.                                                 + data[i];
  41.                         }
  42.                 }
  43.         }
  44. }
复制代码






点评

ok!  发表于 2015-3-15 12:50
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马