JConsole does not connect when a program (running in eclipse) is waiting for a console input

Posted by Calm Storm on Stack Overflow See other posts from Stack Overflow or by Calm Storm
Published on 2010-03-15T13:54:08Z Indexed on 2010/03/16 9:56 UTC
Read the original article Hit count: 345

Filed under:
|
|

Hi, I have a problem similar to the one mentioned here. I have a program which launches a selenium browser and refreshes it every 2 seconds. My idea here was to monitor the memory usage of my application as the page is being hit. This console program does a "System.in.read" and when a user presses enter key, this stops the browser refresh thread. I run the program and it runs as I would expect it to. But when I fire jconsole and attach it to the process, it looks like jconsole waits forever to attach. This is because of the "System.in.read", apparently as shown in the JConsole source. In my code it makes perfect sense for me to terminte the browser when user presses something so is there any alternative available at all ? (Posted code sample below)

package com.ekanathk;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class MultiThread {

    @Test
    public void testSimple() throws Exception {
        List<Integer> x = new ArrayList<Integer>();
        for(int i = 0; i < 1000; i++) {
            x.add(i);
        }
        System.out.print("Press Enter to stop (try attach a jconsole now....");
        System.in.read();
    }
}

The problem here seems to be that any System.in.read (either in Main thread or any threads) seems to completely block jconsole. I guess the problem essentially boils down to multiple threads requesting system inputs at the same time? Is there a solution ?

UPDATE 1: It seems this works fine when launching the class from a command line, however when I run from eclipse, jconsole cant connect. Any reasons ?

© Stack Overflow or respective owner

Related posts about jconsole

Related posts about java