java.lang.Thread.run(Thread.java:745)

时间:2016-08-25 03:42:53

标签: java multithreading

当我使用循环语句运行两个线程时,它向我显示了这个问题:java.lang.Thread.run(Thread.java:745) 但我想通了两种方法: 首先,如果我不运行应用程序只是调试它并使用断点,它会没问题。 第二个基于我的演示只有两个线程所以我可以复制这部分代码"

    new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt();
                        System.out.println(Thread.currentThread().getName()
                                + " has put data:" + data);
                        threadDataMap.put(Thread.currentThread(), data);
                        new A().get();
                        new B().get();
                    }
                }).start();

"

两次也没关系。

我的所有演示代码如下:

    package 多线程;

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

    public class ThreadScopShareData {

    private static int data = 0;
    private static Map<Thread,Integer> threadDataMap=new HashMap<Thread,Integer>();
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread().getName()
                            + " has put data:" + data);
                    threadDataMap.put(Thread.currentThread(), data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }

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

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

正确的结果应如下所示:     Thread-1已放入数据:-1188249922     Thread-0放置了数据:1024718434     来自Thread-0的A获取数据:1024718434     来自Thread-0的B获取数据:1024718434     来自Thread-1的A获取数据:-1188249922     来自Thread-1的B获取数据:-1188249922

但错误如下:Thread-1已放置数据:-807205084    Thread-0放置了数据:-976943511    来自Thread-0的A获取数据:-976943511

   Exception in thread "Thread-1" B from Thread-0 get data :-976943511
    java.lang.NullPointerException
    at 多线程.ThreadScopeShareData$A.get(ThreadScopeShareData.java:33)
    at 多线程.ThreadScopeShareData$1.run(ThreadScopeShareData.java:22)
    at java.lang.Thread.run(Thread.java:745)

据我所知,Thread-1会完全改变并杀死线程-0所以它会出现nullPointerException。但我不确定。

2 个答案:

答案 0 :(得分:1)

由于您在多个主题中使用npm ERR! git fetch -a origin (https://github.com/mtunique/react-native-scrollable-tab-view.git) fatal: Unable to find remote helper for 'https' npm ERR! Darwin 15.4.0 npm ERR! argv "/usr/local/Cellar/node/5.4.0/bin/node" "/usr/local/bin/npm" "install" npm ERR! node v5.4.0 npm ERR! npm v3.3.12 npm ERR! code 128 npm ERR! Command failed: git fetch -a origin npm ERR! fatal: Unable to find remote helper for 'https' npm ERR! npm ERR! npm ERR! If you need help, you may report this error at: npm ERR! <https://github.com/npm/npm/issues> npm ERR! Please include the following file with any support request: npm ERR! /Users/**/npm-debug.log ,请使用threadDataMap代替ConcurrentHashMap

答案 1 :(得分:0)

这可能是因为处理中的系统偏好设置语言设置,将其从中文更改为英语。