java.lang.Thread.run(Thread.java:745)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39136347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.Thread.run(Thread.java:745)
提问by qiaopan Ma
When i use cycle sentence to run two Threads it shows me that problem:java.lang.Thread.run(Thread.java:745) but i figure it out use two methods:\ first if i dont run the application just debug it and use breakpoint it would be ok . second based on my demo just has two threads so i could copy this part of code "
当我使用循环语句运行两个线程时,它向我展示了这个问题: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();
"
for twice it would also be ok .
两次也可以。
all my demo code is below:
我所有的演示代码如下:
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);
}
}
}
and the right result should be like below: Thread-1 has put data :-1188249922 Thread-0 has put data :1024718434 A from Thread-0 get data :1024718434 B from Thread-0 get data :1024718434 A from Thread-1 get data :-1188249922 B from Thread-1 get data :-1188249922
正确的结果应该如下所示: Thread-1 has put data :-1188249922 Thread-0 has put data :1024718434 A from Thread-0 get data :1024718434 B from Thread-0 get data :1024718434 A from Thread-1 get数据:-1188249922 B 从线程 1 获取数据:-1188249922
but the bug is below:Thread-1 has put data :-807205084 Thread-0 has put data :-976943511 A from Thread-0 get data :-976943511
但错误如下:Thread-1 has put data :-807205084 Thread-0 has put data :-976943511 A from Thread-0 get data :-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.run(ThreadScopeShareData.java:22)
at java.lang.Thread.run(Thread.java:745)
As far as i know the Thread-1 would totally change and kill the thread-0 so it would appear a nullPointerException . but i am not sure.
据我所知, Thread-1 会完全改变并杀死 thread-0,因此它会出现 nullPointerException 。但我不确定。
回答by Konstantin Labun
Since you use threadDataMap
in multiple threads use ConcurrentHashMap
instead of HashMap
.
由于您threadDataMap
在多个线程中使用ConcurrentHashMap
而不是HashMap
.
回答by ShadeBai
It's probably because of your System Preferences language setting in Processing, change it from Chinese to English.
这可能是因为您在Processing中的 System Preferences 语言设置,将其从中文更改为英文。