Rohan
Rohan

Reputation: 561

program is not ending smoothly in multithreading

I am creating some threads in my program .Here i used join method so that main thread will wait for all my threads.But whenever i am running this program it is not getting completed all the info messages getting printed but still program not ends smoothly.Can anybody help me to come out of this?

Below is my code:

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");

Below is the code for 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");



        }
    }

Below is the thread dump:

Full thread dump Java HotSpot(TM) Server VM (1.5.0_30-b03 mixed mode):

"DestroyJavaVM" prio=3 tid=0x00037840 nid=0x1 waiting on condition [0x00000000..0xffbfe3d0]

"Connect thread 172.24.85.166 session" prio=3 tid=0x006bcdb0 nid=0x25 runnable [0xb29af000..0xb29afbf0] at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at com.jcraft.jsch.IO.getByte(IO.java:82) at com.jcraft.jsch.Session.read(Session.java:860) at com.jcraft.jsch.Session.run(Session.java:1330) at java.lang.Thread.run(Thread.java:637)

"Low Memory Detector" daemon prio=3 tid=0x002e6fc8 nid=0x22 runnable [0x00000000..0x00000000]

"CompilerThread1" daemon prio=3 tid=0x002e5130 nid=0x21 waiting on condition [0x00000000..0xb3aee9bc]

"CompilerThread0" daemon prio=3 tid=0x002e4310 nid=0x20 waiting on condition [0x00000000..0xb3b7ea84]

"AdapterThread" daemon prio=3 tid=0x002e34f0 nid=0x1f waiting on condition [0x00000000..0x00000000]

"Signal Dispatcher" daemon prio=3 tid=0x002e2738 nid=0x1e waiting on condition [0x00000000..0x00000000]

"Finalizer" daemon prio=3 tid=0x002d8d68 nid=0x1d in Object.wait() [0xb3d2f000..0xb3d2fbf0] at java.lang.Object.wait(Native Method) - waiting on <0xe3001418> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:120) - locked <0xe3001418> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:136) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=3 tid=0x002d7648 nid=0x1c in Object.wait() [0xb3dbf000..0xb3dbfc70] at java.lang.Object.wait(Native Method) - waiting on <0xe3001218> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:474) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116) - locked <0xe3001218> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=3 tid=0x002d3c60 nid=0x1b runnable

"GC task thread#0 (ParallelGC)" prio=3 tid=0x000ba278 nid=0x2 runnable

"GC task thread#1 (ParallelGC)" prio=3 tid=0x000bacc0 nid=0x3 runnable

"GC task thread#2 (ParallelGC)" prio=3 tid=0x000bb708 nid=0x4 runnable

"GC task thread#3 (ParallelGC)" prio=3 tid=0x000bc150 nid=0x5 runnable

"GC task thread#4 (ParallelGC)" prio=3 tid=0x000bcb98 nid=0x6 runnable

"GC task thread#5 (ParallelGC)" prio=3 tid=0x000bd5e0 nid=0x7 runnable

"GC task thread#6 (ParallelGC)" prio=3 tid=0x000be830 nid=0x8 runnable

"GC task thread#7 (ParallelGC)" prio=3 tid=0x000bf278 nid=0x9 runnable

"GC task thread#8 (ParallelGC)" prio=3 tid=0x000bfcc0 nid=0xa runnable

"GC task thread#9 (ParallelGC)" prio=3 tid=0x000c0730 nid=0xb runnable

"GC task thread#10 (ParallelGC)" prio=3 tid=0x000c1178 nid=0xc runnable

"GC task thread#11 (ParallelGC)" prio=3 tid=0x000c1bc0 nid=0xd runnable

"GC task thread#12 (ParallelGC)" prio=3 tid=0x000c2608 nid=0xe runnable

"GC task thread#13 (ParallelGC)" prio=3 tid=0x000c3050 nid=0xf runnable

"GC task thread#14 (ParallelGC)" prio=3 tid=0x000c3a98 nid=0x10 runnable

"GC task thread#15 (ParallelGC)" prio=3 tid=0x000c44e0 nid=0x11 runnable

"GC task thread#16 (ParallelGC)" prio=3 tid=0x000c4f30 nid=0x12 runnable

"GC task thread#17 (ParallelGC)" prio=3 tid=0x000c5978 nid=0x13 runnable

"GC task thread#18 (ParallelGC)" prio=3 tid=0x000c63c0 nid=0x14 runnable

"GC task thread#19 (ParallelGC)" prio=3 tid=0x000c6e08 nid=0x15 runnable

"GC task thread#20 (ParallelGC)" prio=3 tid=0x000c7850 nid=0x16 runnable

"GC task thread#21 (ParallelGC)" prio=3 tid=0x000c8298 nid=0x17 runnable

"GC task thread#22 (ParallelGC)" prio=3 tid=0x000c8ce0 nid=0x18 runnable

"GC task thread#23 (ParallelGC)" prio=3 tid=0x000c9728 nid=0x19 runnable

"GC task thread#24 (ParallelGC)" prio=3 tid=0x000ca170 nid=0x1a runnable

"VM Periodic Task Thread" prio=3 tid=0x000b1820 nid=0x23 waiting on condition

Upvotes: 1

Views: 1016

Answers (1)

gma
gma

Reputation: 2563

It seems that your ssh seesion opened with JSCH is not terminated ; a non daemon thread Connect thread is still alive. If as you said your program is running normally, be sure to disconnect from the jsch session.

You can also set the DaemonThread property on the session to true to avoid the hanging caused by the ConnectThread session.setDaemonThread(true). Has to be invoked before connect().

Upvotes: 2

Related Questions