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

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

如下代码中会出现问题

package cn.itcast.heima2;

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

public class ThreadScopeShareData {

    private static int data = http://www.mamicode.com/0;

    private static HashMap<Thread, Integer> threadData = http://www.mamicode.com/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 = http://www.mamicode.com/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=http://www.mamicode.com/threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    static class B {
        public void get() {
            int data=http://www.mamicode.com/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 = http://www.mamicode.com/0;
    static long[] seed = new long[] { 12345612, 654321 };
    private static HashMap<Thread, Integer> threadData = http://www.mamicode.com/new HashMap<>();

    public static void main(String[] args) {

        new Thread(new ThreadScopeShareData.MyRunnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                data =http://www.mamicode.com/ 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 =http://www.mamicode.com/ 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=http://www.mamicode.com/threadData.get(Thread.currentThread());
            System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data);
        }
    }

    static class B {
        public void get() {
             int data=http://www.mamicode.com/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 线程范围内共享变量的概念与作用