HttpURLConnection spawns a large number of processes?

I have some pretty simple code to get the HTTP status code from a given url:

URL url = new URL(args[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); connection.setInstanceFollowRedirects(true); int code = connection.getResponseCode(); connection.disconnect(); System.out.print(code); System.exit(0); 

Obviously, everything is inside try / catch blocks, but all they do is exit with an error. The code seemed to work, so I ran a list of urls. I tracked processes and noticed that some 10 Java instances were created on some URLs for the same URL.

In other words, I would run:

 java -jar HTTP.jar {URL} 

and see this command about 10 times when I started htop. They look like regular processes, not threads, but in htop I "hide threads of user threads". What's happening? Passed multiple requests or only one?

+4
source share
1 answer

You accidentally run the command several times. This can happen if you could not escape the & in the url in the shell command. Bash interprets the & symbol as meaning "run this command in the background" and you will get a shell prompt back, but your process is still running.

Put the URL in single quotes and you won’t have any problems.

0
source

All Articles