我在程序中创建了一些线程。在这里,我使用了join方法,以便主线程将等待所有线程。但是,每当我运行该程序时,它的所有信息消息都无法完成打印,但程序仍然无法顺利结束。有人可以帮助我摆脱困境吗?
下面是我的代码:
ArrayList<DeployerThread> threadsarrforjoin = new ArrayList<DeployerThread>();
for (int i = 0; i < serverList.size(); i++) {
logger.debug("Inside for loop of buildParameterList()");
if (serverList.get(i).ServerType.toString().equalsIgnoreCase("SS") &&
serverList.get(i).GroupNo.toString().equalsIgnoreCase(groupNo)) {
try {
nameofthreads.add("PhoinexHyperlink[" +
serverList.get(i).ServerIP + "_" +
serverList.get(i).ServerType + "]");
DeployerThread deployerThread = new DeployerThread(serverList.get(i), action);
logger.info("---- Start thread execution ---- ");
deployerThread.start();
threadsarrforjoin.add(deployerThread);
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
this.logger.error(e.toString());
}
count++;
}
}
for (int i = 0; i < threadsarrforjoin.size(); i++) {
try {
this.threadsarrforjoin.get(i).join();
} catch (InterruptedException ex) {
logger.error(ex.getMessage());
Logger.getLogger(StartStopWebServers.class.getName()).log(Level.SEVERE, null, ex);
}
}
logger.info(taskName + " Completed");
以下是DeployerThread的代码:
public DeployerThread(ServerConfig serverConfig, String actn, String WEBPROC, String WEBPROC1, String WEBPROCIgnore) {
logger.debug("Inside DeployerThread constructor");
logger.debug("serverConfig=" + serverConfig);
logger.debug("Action=" + actn);
setServerConfig(serverConfig);
setTaskaction(actn);
setWebproc(WEBPROC);
setWebproc1(WEBPROC1);
setWebprocignore(WEBPROCIgnore);
}
public final ServerConfig getServerConfig() {
return serverConfig;
}
public final void setServerConfig(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
public void run() {
logger.debug("Inside threads run method : Start");
count++;
logger.debug("Thread number >>>>" + count + "<<<<");
Thread.currentThread().setName("[" + serverConfig.ServerIP + "_" + serverConfig.ServerType + "]");
// threadsarrforjoin.add(Thread.currentThread());
logger.debug("Name given to this thread=" + "[" + serverConfig.ServerIP + "_" + serverConfig.ServerType + "]");
org.slf4j.MDC.put("userid", buildPath + "/" + taskName + "/" + taskaction + "_" + Thread.currentThread().getName() + ".log");
logger.info("Deployment started on " + serverConfig.ServerIP + " for " + serverConfig.ServerType + " at " + dateTimeStamp);
logger.debug("taskname=" + taskName + "_" + serverConfig.ServerType);
logger.debug("buildPath=" + buildPath);
runSSH(serverConfig, action, webproc, webproc1, webprocignore);
logger.debug("Outside threads run method : End");
}
}
下面是线程转储:
全线程转储Java HotSpot(TM)服务器VM(1.5.0_30-b03混合模式):
“ DestroyJavaVM” prio = 3 tid = 0x00037840 nid = 0x1等待条件[0x00000000..0xffbfe3d0]
“连接线程172.24.85.166会话” prio = 3 tid = 0x006bcdb0 nid = 0x25可运行[0xb29af000..0xb29afbf0]
在java.net.SocketInputStream.socketRead0(本地方法)
在java.net.SocketInputStream.read(SocketInputStream.java:129)
在com.jcraft.jsch.IO.getByte(IO.java:82)
在com.jcraft.jsch.Session.read(Session.java:860)
在com.jcraft.jsch.Session.run(Session.java:1330)
在java.lang.Thread.run(Thread.java:637)
“内存不足检测器”守护程序prio = 3 tid = 0x002e6fc8 nid = 0x22可运行[0x00000000..0x00000000]
“ CompilerThread1”守护程序prio = 3 tid = 0x002e5130 nid = 0x21等待条件[0x00000000..0xb3aee9bc]
“ CompilerThread0”守护程序prio = 3 tid = 0x002e4310 nid = 0x20等待条件[0x00000000..0xb3b7ea84]
“ AdapterThread”守护程序prio = 3 tid = 0x002e34f0 nid = 0x1f等待条件[0x00000000..0x00000000]
“ Signal Dispatcher”守护程序prio = 3 tid = 0x002e2738 nid = 0x1e等待条件[0x00000000..0x00000000]
Object.wait()[0xb3d2f000..0xb3d2fbf0]中的“ Finalizer”守护程序prio = 3 tid = 0x002d8d68 nid = 0x1d
在java.lang.Object.wait(本地方法)
-等待(java.lang.ref.ReferenceQueue $ Lock)
在java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:120)
-锁定(java.lang.ref.ReferenceQueue $ Lock)
在java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:136)
在java.lang.ref.Finalizer $ FinalizerThread.run(Finalizer.java:159)
Object.wait()中的“引用处理程序”守护程序prio = 3 tid = 0x002d7648 nid = 0x1c [0xb3dbf000..0xb3dbfc70]
在java.lang.Object.wait(本地方法)
-等待(java.lang.ref.Reference $ Lock)
在java.lang.Object.wait(Object.java:474)
在java.lang.ref.Reference $ ReferenceHandler.run(Reference.java:116)
-锁定(java.lang.ref.Reference $ Lock)
“ VM线程” prio = 3 tid = 0x002d3c60 nid = 0x1b可运行
“ GC任务线程#0(ParallelGC)” prio = 3 tid = 0x000ba278 nid = 0x2可运行
“ GC任务线程#1(ParallelGC)” prio = 3 tid = 0x000bacc0 nid = 0x3可运行
“ GC任务线程#2(ParallelGC)” prio = 3 tid = 0x000bb708 nid = 0x4可运行
“ GC任务线程#3(ParallelGC)” prio = 3 tid = 0x000bc150 nid = 0x5可运行
“ GC任务线程#4(ParallelGC)” prio = 3 tid = 0x000bcb98 nid = 0x6可运行
“ GC任务线程#5(ParallelGC)” prio = 3 tid = 0x000bd5e0 nid = 0x7可运行
“ GC任务线程#6(ParallelGC)” prio = 3 tid = 0x000be830 nid = 0x8可运行
“ GC任务线程#7(ParallelGC)” prio = 3 tid = 0x000bf278 nid = 0x9可运行
“ GC任务线程#8(ParallelGC)” prio = 3 tid = 0x000bfcc0 nid = 0xa可运行
“ GC任务线程#9(ParallelGC)” prio = 3 tid = 0x000c0730 nid = 0xb可运行
“ GC任务线程#10(ParallelGC)” prio = 3 tid = 0x000c1178 nid = 0xc可运行
“ GC任务线程#11(ParallelGC)” prio = 3 tid = 0x000c1bc0 nid = 0xd可运行
“ GC任务线程#12(ParallelGC)” prio = 3 tid = 0x000c2608 nid = 0xe可运行
“ GC任务线程#13(ParallelGC)” prio = 3 tid = 0x000c3050 nid = 0xf可运行
“ GC任务线程#14(ParallelGC)” prio = 3 tid = 0x000c3a98 nid = 0x10可运行
“ GC任务线程#15(ParallelGC)” prio = 3 tid = 0x000c44e0 nid = 0x11可运行
“ GC任务线程#16(ParallelGC)” prio = 3 tid = 0x000c4f30 nid = 0x12可运行
“ GC任务线程#17(ParallelGC)” prio = 3 tid = 0x000c5978 nid = 0x13可运行
“ GC任务线程#18(ParallelGC)” prio = 3 tid = 0x000c63c0 nid = 0x14可运行
“ GC任务线程#19(ParallelGC)” prio = 3 tid = 0x000c6e08 nid = 0x15可运行
“ GC任务线程#20(ParallelGC)” prio = 3 tid = 0x000c7850 nid = 0x16可运行
“ GC任务线程#21(ParallelGC)” prio = 3 tid = 0x000c8298 nid = 0x17可运行
“ GC任务线程#22(ParallelGC)” prio = 3 tid = 0x000c8ce0 nid = 0x18可运行
“ GC任务线程#23(ParallelGC)” prio = 3 tid = 0x000c9728 nid = 0x19可运行
“ GC任务线程#24(ParallelGC)” prio = 3 tid = 0x000ca170 nid = 0x1a可运行
“ VM定期任务线程” prio = 3 tid = 0x000b1820 nid = 0x23等待条件
最佳答案
用JSCH打开的ssh似乎没有终止;非守护程序线程Connect thread
仍然有效。如果您说过程序正常运行,请确保从jsch会话断开连接。
您还可以将会话上的DaemonThread
属性设置为true,以避免由ConnectThread session.setDaemonThread(true)
引起的挂起。必须在connect()
之前调用。