程序在多线程中没有顺利结束

时间:2013-05-24 06:20:34

标签: java multithreading

我正在我的程序中创建一些线程。这里我使用了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 runnable [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 runnable [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]

“Finalizer”守护程序prio = 3 tid = 0x002d8d68 nid = 0x1d在Object.wait()[0xb3d2f000..0xb3d2fbf0]         at java.lang.Object.wait(Native Method)          - 等待&lt; 0xe3001418&gt; (java.lang.ref.ReferenceQueue $ Lock)         at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:120)          - 已锁定&lt; 0xe3001418&gt; (java.lang.ref.ReferenceQueue $ Lock)         at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:136)         在java.lang.ref.Finalizer $ FinalizerThread.run(Finalizer.java:159)

“参考处理程序”守护程序prio = 3 tid = 0x002d7648 nid = 0x1c in Object.wait()[0xb3dbf000..0xb3dbfc70]         at java.lang.Object.wait(Native Method)          - 等待&lt; 0xe3001218&gt; (java.lang.ref.Reference $ Lock)         在java.lang.Object.wait(Object.java:474)         在java.lang.ref.Reference $ ReferenceHandler.run(Reference.java:116)          - 已锁定&lt; 0xe3001218&gt; (java.lang.ref.Reference $ Lock)

“VM Thread”prio = 3 tid = 0x002d3c60 nid = 0x1b runnable

“GC任务线程#0(ParallelGC)”prio = 3 tid = 0x000ba278 nid = 0x2 runnable

“GC任务线程#1(ParallelGC)”prio = 3 tid = 0x000bacc0 nid = 0x3 runnable

“GC任务线程#2(ParallelGC)”prio = 3 tid = 0x000bb708 nid = 0x4 runnable

“GC任务线程#3(ParallelGC)”prio = 3 tid = 0x000bc150 nid = 0x5 runnable

“GC任务线程#4(ParallelGC)”prio = 3 tid = 0x000bcb98 nid = 0x6 runnable

“GC任务线程#5(ParallelGC)”prio = 3 tid = 0x000bd5e0 nid = 0x7 runnable

“GC任务线程#6(ParallelGC)”prio = 3 tid = 0x000be830 nid = 0x8 runnable

“GC任务线程#7(ParallelGC)”prio = 3 tid = 0x000bf278 nid = 0x9 runnable

“GC任务线程#8(ParallelGC)”prio = 3 tid = 0x000bfcc0 nid = 0xa runnable

“GC任务线程#9(ParallelGC)”prio = 3 tid = 0x000c0730 nid = 0xb runnable

“GC任务线程#10(ParallelGC)”prio = 3 tid = 0x000c1178 nid = 0xc runnable

“GC任务线程#11(ParallelGC)”prio = 3 tid = 0x000c1bc0 nid = 0xd runnable

“GC任务线程#12(ParallelGC)”prio = 3 tid = 0x000c2608 nid = 0xe runnable

“GC任务线程#13(ParallelGC)”prio = 3 tid = 0x000c3050 nid = 0xf runnable

“GC任务线程#14(ParallelGC)”prio = 3 tid = 0x000c3a98 nid = 0x10 runnable

“GC任务线程#15(ParallelGC)”prio = 3 tid = 0x000c44e0 nid = 0x11 runnable

“GC任务线程#16(ParallelGC)”prio = 3 tid = 0x000c4f30 nid = 0x12 runnable

“GC任务线程#17(ParallelGC)”prio = 3 tid = 0x000c5978 nid = 0x13 runnable

“GC任务线程#18(ParallelGC)”prio = 3 tid = 0x000c63c0 nid = 0x14 runnable

“GC任务线程#19(ParallelGC)”prio = 3 tid = 0x000c6e08 nid = 0x15 runnable

“GC任务线程#20(ParallelGC)”prio = 3 tid = 0x000c7850 nid = 0x16 runnable

“GC任务线程#21(ParallelGC)”prio = 3 tid = 0x000c8298 nid = 0x17 runnable

“GC任务线程#22(ParallelGC)”prio = 3 tid = 0x000c8ce0 nid = 0x18 runnable

“GC任务线程#23(ParallelGC)”prio = 3 tid = 0x000c9728 nid = 0x19 runnable

“GC任务线程#24(ParallelGC)”prio = 3 tid = 0x000ca170 nid = 0x1a runnable

“VM Periodic Task Thread”prio = 3 tid = 0x000b1820 nid = 0x23等待条件

1 个答案:

答案 0 :(得分:2)

看来你用JSCH打开的ssh seeion没有被终止;非守护程序线程Connect thread仍然存在。如果您说您的程序正常运行,请务必断开与jsch会话的连接。

您还可以将会话中的DaemonThread属性设置为true,以避免ConnectThread session.setDaemonThread(true)导致挂起。必须在connect()之前调用。