package com.nodic.test;
import java.util.Random;
public class StopThread {
private static User user = new User();
public static class User {
private int id;
private String name;
public User() {
id = 1;
name = "1";
}
@Override
public String toString() {
return "{id=" + id + ", name=\"" + name + "\"}";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public static class WriteThread implements Runnable {
@Override
public void run() {
while(true) {
synchronized (user) {
int curr_id = new Random().nextInt();
user.setId(curr_id);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
user.setName(String.valueOf(curr_id));
}
Thread.yield();
}
}
}
public static class ReadThread implements Runnable {
@Override
public void run() {
while (true) {
synchronized (user) {
if(!user.getName().equals(String.valueOf(user.getId()))) {
System.out.println(user.toString());
break;
}
}
Thread.yield();
}
}
}
public static void main(String[] args) {
new Thread(new ReadThread(), "Thread-StopThread-ReadThread").start();
int count = 0;
while(count < 50) {
Thread t = new Thread(new WriteThread(), "Thread-StopThread-WriteThread");
t.start();
t.stop();
count ++;
}
}
}
每个人,当我用jdk1.8.0_45运行这个类时,为什么我总是遇到这个错误:java.lang.NoClassDefFoundError:无法初始化类java.util.Random
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java -Didea.launcher.port = 7534&#34; -Didea.launcher.bin.path = / Applications / IDE / IntelliJ IDEA / IntelliJ IDEA 14.app/Contents/bin" -Dfile.encoding = UTF-8 -classpath &#34; /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/ dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/ jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/sa- jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/ LIB / javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/ JRE / lib目录/ jfr.jar:/图书馆/的Java / JavaVirtual机/ jdk1.8.0_45.jdk /内容/首页/ JRE / lib中/ jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jsse.jar:/库/的Java / JavaVirtualMachines / jdk1.8.0_45.jdk /内容/首页/ JRE / lib目录/管理-agent.jar中:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/plugin.jar :/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/rt的.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre /lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk /Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/ jdk1.8.0_45.jdk /内容/首页/ JRE / lib / ext目录/ SUNE c.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/ JRE / lib / ext目录/ sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/用户/ huangshiqian /工作区/ IDEA /测试/并行/target/classes:/Users/huangshiqian/.m2/repository/junit/junit/4.9/junit-4.9.jar:/Users/huangshiqian/.m2/repository/org/hamcrest/hamcrest-core/1.1/hamcrest-core -1.1.jar:/应用/ IDE /的IntelliJ IDEA / IntelliJ IDEA 14.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.nodic.test.StopThread
异常:抛出java.lang.ThreadDeath 线程中的UncaughtExceptionHandler&#34; Thread-StopThread-WriteThread&#34;
异常:抛出java.lang.ThreadDeath 线程中的UncaughtExceptionHandler&#34; Thread-StopThread-WriteThread&#34; 线程中的异常&#34; Thread-StopThread-WriteThread&#34;例外 thread&#34; Thread-StopThread-WriteThread&#34;例外: 从UncaughtExceptionHandler中抛出的java.lang.ThreadDeath thread&#34; Thread-StopThread-WriteThread&#34;线程中的异常 &#34;螺纹StopThread-WriteThread&#34;线程中的异常 &#34;螺纹StopThread-WriteThread&#34;线程中的异常 &#34;螺纹StopThread-WriteThread&#34;线程中的异常 &#34;螺纹StopThread-WriteThread&#34;例外:java.lang.ThreadDeath 从线程中的UncaughtExceptionHandler抛出 &#34;螺纹StopThread-WriteThread&#34;
异常:抛出java.lang.ThreadDeath 线程中的UncaughtExceptionHandler&#34; Thread-StopThread-WriteThread&#34;
异常:抛出java.lang.ThreadDeath 线程中的UncaughtExceptionHandler&#34; Thread-StopThread-WriteThread&#34;
异常:抛出java.lang.ThreadDeath 线程中的UncaughtExceptionHandler&#34; Thread-StopThread-WriteThread&#34;
异常:抛出java.lang.ThreadDeath 线程中的UncaughtExceptionHandler&#34; Thread-StopThread-WriteThread&#34; 线程中的异常&#34; Thread-StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:无法初始化类 java.util.Random at com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)at at java.lang.Thread.run(Thread.java:745)线程中的异常 &#34;螺纹StopThread-WriteThread&#34; java.lang.NoClassDefFoundError:可以 不初始化java.util.Random类 com.nodic.test.StopThread $ WriteThread.run(StopThread.java:44)
答案 0 :(得分:2)
在运行stop()
类的静态初始化时,您很可能在线程上调用Random
。
JVM只尝试初始化一次类。
如果类初始化失败(因为抛出了异常),JVM会将其标记,并且任何依赖它的类......都是“坏”。如果您尝试使用它/它们,则会得到ClassDefNotFoundError
。
Thread.stop()
的实现是导致ThreadDeath
异常被抛出到目标线程的堆栈上。
简而言之,这是一个说明调用Thread.stop()
不安全的原因的例子。