码迷,mamicode.com
首页 > 编程语言 > 详细

(黑马Java多线程与并发库高级应用)05 线程范围内共享变量的概念与作用

时间:2017-07-24 14:48:46      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:add   ash   rgs   print   rac   str   线程   多线程   dom   

如下代码中会出现问题

package cn.itcast.heima2;

import java.util.HashMap;
import java.util.Random;

public class ThreadScopeShareData {

    private static int data = 0;

    private static HashMap<Thread, Integer> threadData = new HashMap<>();

    public static void main(String[] args) {

        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {

                 @Override
                public void run() {
                    // TODO Auto-generated method stub
                    data = new Random(System.currentTimeMillis()).nextInt();
                    System.out.println(Thread.currentThread().getName() + " has put data :" + data);
                    threadData.put(Thread.currentThread(), data);
                    new A().get();
                    new B().get();
                }

            }).start();

        }
    }

    static class A {
        public void get() {
            int data=threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    static class B {
        public void get() {
            int data=threadData.get(Thread.currentThread());
            System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

}
 

解决方法:

package cn.itcast.heima2;

import java.util.HashMap;
import java.util.Random;

public class ThreadScopeShareData {

    private static int data = 0;
    static long[] seed = new long[] { 12345612, 654321 };
    private static HashMap<Thread, Integer> threadData = new HashMap<>();

    public static void main(String[] args) {

        new Thread(new ThreadScopeShareData.MyRunnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                data = MyRunnable.rd.nextInt();
                System.out.println(Thread.currentThread().getName() + " has put data :" + data);
                 threadData.put(Thread.currentThread(), data);
                new A().get();
                new B().get();
            }

        }).start();

        new Thread(new ThreadScopeShareData.MyRunnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                data = MyRunnable.rd.nextInt();
                System.out.println(Thread.currentThread().getName() + " has put data :" + data);
                 threadData.put(Thread.currentThread(), data);
                new A().get();
                new B().get();
            }

        }).start();

    }

    static class A {
        public void get() {
             int data=threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    static class B {
        public void get() {
             int data=threadData.get(Thread.currentThread());
            System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    abstract static class MyRunnable implements Runnable {
        static Random rd = new Random();

        public abstract void run();

    }

}

 

(黑马Java多线程与并发库高级应用)05 线程范围内共享变量的概念与作用

标签:add   ash   rgs   print   rac   str   线程   多线程   dom   

原文地址:http://www.cnblogs.com/404-not-found/p/7228288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!