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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(黑马Java多线程与并发库高级应用)05 线程范围内共享变量的概念与作用相关的知识,希望对你有一定的参考价值。

如下代码中会出现问题

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 线程范围内共享变量的概念与作用的主要内容,如果未能解决你的问题,请参考以下文章

(黑马Java多线程与并发库高级应用)04 传统线程同步通信技术

(黑马Java多线程与并发库高级应用)02 传统定时器技术回顾

Java多线程与并发库高级应用-java5线程并发库

Java多线程与并发库高级应用-线程池

Java多线程与并发库高级应用-传统线程互斥技术

Java多线程与并发库高级应用