我有一个非常复杂的 Java 程序,它不会终止。 Eclipse 调试器显示一个可以暂停的线程,但没有堆栈跟踪。 它被称为“Thread-2”。
此线程的 jstack -l 输出是:
"Thread-2" #17 prio=5 os_prio=0 tid=0x00007f1268002800 nid=0x3342 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
我在 Thread.start() 中添加了断点,但找不到名为“Thread-2”的线程。 该线程仅在创建两个“AWT-Event-Queue”线程之后出现。 我不在我的程序中手动创建任何线程。
在主线程和所有其他线程退出,并释放 JFrame 后,以下线程仍然存在:
Thread [AWT-EventQueue-0] (Running)
Thread [Thread-2] (Running)
Thread [DestroyJavaVM] (Running)
挂起VM时,存在以下线程:
Daemon System Thread [Signal Dispatcher] (Suspended)
Daemon System Thread [Finalizer] (Suspended)
Daemon System Thread [Reference Handler] (Suspended)
Daemon System Thread [Java2D Disposer] (Suspended)
Daemon System Thread [AWT-XAWT] (Suspended)
Thread [AWT-EventQueue-0] (Suspended)
Thread [Thread-2] (Suspended)
Thread [DestroyJavaVM] (Suspended)
我怎样才能得到关于这个线程的更多信息,或者让它终止?
编辑 1:
根据eclipse pom.xml View 的Dependency Hierarchy,我使用了以下第三方库:
guava 17.0 [compile]
hamcrest-core 1.3 [test]
junit 4.11 [test]
log4j-api 2.0-beta9 [compile]
log4j-core 2.0-beta9 [compile]
编辑 2:
按照https://stackoverflow.com/a/35128213/577485中的建议,为线程类的所有构造函数添加断点,我看到 Thread-0 和 Thread-1 是由 log4j 创建的,但不是 Thread-2。它仍然像以前一样出现,并且在构造时没有断点触发。
编辑 3:
现在它变得令人毛骨悚然。在线程上调用时,甚至 stop() 方法也不起作用。我将它添加到 https://stackoverflow.com/a/35128149/577485 中给出的代码中.
至少 System.exit(int) 仍然有效。但正如评论中所说,我不想使用它。
编辑 4:
关于我的系统的信息:
security、updates 和 backports 存储库。Java 版本“1.7.0_91”
OpenJDK 运行时环境 (IcedTea 2.6.3) (7u91-2.6.3-0ubuntu0.15.10.1)
OpenJDK 64 位服务器 VM(构建 24.91-b01,混合模式)
编辑 5:
我用直接从java.com下载的Java版本jre-8u71-linux-x64执行程序,但错误依旧。 jstack -l 显示相同的奇怪线程。请注意,该程序仍然是使用较旧的 Java 版本构建的。 编辑:在使用 oracle.com 的 java8u72 编译它之后,我得到了相同的行为。
编辑 6:
我遍历了所有线程字段,这是输出。我无法从这些字段中得到任何提示,线程甚至没有目标。
name: [C@6b67034
priority: 5
threadQ: null
eetop: 140274638530560
single_step: false
daemon: false
stillborn: false
target: null
group: java.lang.ThreadGroup[name=main,maxpri=10]
contextClassLoader: null
inheritedAccessControlContext: java.security.AccessControlContext@0
threadInitNumber: 3
threadLocals: null
inheritableThreadLocals: null
stackSize: 0
nativeParkEventPointer: 0
tid: 17
threadSeqNumber: 20
threadStatus: 5
parkBlocker: null
blocker: null
blockerLock: java.lang.Object@16267862
MIN_PRIORITY: 1
NORM_PRIORITY: 5
MAX_PRIORITY: 10
EMPTY_STACK_TRACE: [Ljava.lang.StackTraceElement;@453da22c
SUBCLASS_IMPLEMENTATION_PERMISSION: ("java.lang.RuntimePermission" "enableContextClassLoaderOverride")
uncaughtExceptionHandler: null
defaultUncaughtExceptionHandler: null
threadLocalRandomSeed: 0
threadLocalRandomProbe: 0
threadLocalRandomSecondarySeed: 0
编辑 7:
在 Thread 的 name 字段中添加了一个观察点。它只能由我的分析代码访问,而且似乎从未被写入...
编辑 8:
jstack -F -m 为我的程序抛出一个错误:
Attaching to process ID 10973, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.71-b15
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.tools.jstack.JStack.runJStackTool(JStack.java:140)
at sun.tools.jstack.JStack.main(JStack.java:106)
Caused by: java.lang.RuntimeException: Unable to deduce type of thread from address 0x00007ff68000c000 (expected type JavaThread, CompilerThread, ServiceThread, JvmtiAgentThread, or SurrogateLockerThread)
at sun.jvm.hotspot.runtime.Threads.createJavaThreadWrapper(Threads.java:169)
at sun.jvm.hotspot.runtime.Threads.first(Threads.java:153)
at sun.jvm.hotspot.tools.PStack.initJFrameCache(PStack.java:200)
at sun.jvm.hotspot.tools.PStack.run(PStack.java:71)
at sun.jvm.hotspot.tools.PStack.run(PStack.java:58)
at sun.jvm.hotspot.tools.PStack.run(PStack.java:53)
at sun.jvm.hotspot.tools.JStack.run(JStack.java:66)
at sun.jvm.hotspot.tools.Tool.startInternal(Tool.java:260)
at sun.jvm.hotspot.tools.Tool.start(Tool.java:223)
at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
at sun.jvm.hotspot.tools.JStack.main(JStack.java:92)
... 6 more
Caused by: sun.jvm.hotspot.types.WrongTypeException: No suitable match for type of address 0x00007ff68000c000
at sun.jvm.hotspot.runtime.InstanceConstructor.newWrongTypeException(InstanceConstructor.java:62)
at sun.jvm.hotspot.runtime.VirtualConstructor.instantiateWrapperFor(VirtualConstructor.java:80)
at sun.jvm.hotspot.runtime.Threads.createJavaThreadWrapper(Threads.java:165)
... 16 more
奇怪线程的类名是java.lang.Thread。
我不使用任何命令行参数来执行程序。添加 -Dlog4j2.disable.jmx=true 选项为奇怪的线程提供名称 Thread-1。
我将 log4j 更新到 2.5,奇怪的线程现在有了名称 Thread-0,当 -Dlog4j2.disable.jmx=true 选项已给出,Thread-1,如果未给出。
编辑 9:
完全删除了 log4j,错误仍然存在。该线程现在称为 Thread-0。
Here是项目,如果有帮助的话。
最佳答案
我不明白为什么 Thread.start() 上的断点不起作用,但您也可以尝试通过在 Thread 构造函数上设置断点来拦截线程 >>creation<>Thread.init() 方法上。
线程名为 Thread-2 的事实意味着它是由生成默认线程名称的构造函数之一创建的。这表明它不是由 JVM 或标准 Java 类库创建的。它还缩小了可用于创建它的构造函数的范围。
How can I get more information about this thread ...
除了设置断点,我想不出任何办法。
... or allow it to terminate?
如果您能找到它的创建位置,您应该能够使用 setDaemon(true) 将其标记为守护线程。但是,这需要在线程启动之前完成。
另一种可能性是通过遍历 ThreadGroup 树然后在其上调用 Thread.interrupt() 来找到线程。 (Thread.getAllStackTraces() 是跟踪线程对象的另一种方法。)但是,不能保证线程会“尊重”中断并关闭。
最后,您可以调用 System.exit(...)。
更新
我提到线程可能不遵守 interrupt() 并且我对 stop() 不起作用并不感到惊讶。 (它已被弃用,甚至可能不会在某些平台上实现。)
但是,如果您设法实现了实际找到神秘线程的代码,您可以四处挖掘以找到Thread 子类或Runnable 它被实例化。如果您可以打印出完全限定的类名,那么您就会知道它的来源。 (假设您仍然没有成功设置断点,那么您可能需要使用“讨厌的”反射从线程的私有(private) target 字段中提取可运行对象。)
关于没有堆栈的 Java "Thread-2"会阻止终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35088154/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que