Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread problem  RSS feed

 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fallowing is the modified code from
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html




i need to call runexec(String cmd) method number of times in my programme but when i am calling first time the StreamGobbler.activeCount() is giving 35 and next time when ever i call its incrmenting 2 times but i am calling the runexec(String cmd) after it completes its execution


my question is why intially the activeCount() is showing more than 2 ?
and i want to use two threads that have been created i dont want to increase the number of threads every time i call the method

thanks in advance
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you running this application in some app server? That may account for the number of threads? Can't really answer your question from source code shown.

As for the stream gobbler it looks like it exits when it is done. Maybe it is because the program is still running? Does the output show that it is completed?

Henry
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) yes i am running the application in an application server installed on as400
2) yes the dos command window that opens with java.exec command closes which i think implies that execution of programme complete as well i am getting the needed out put.

do we need to terminate the threads created ?as the thread count is increasing and taking the cpu as well the cpu is not coming down untill server is restarted .

thanks for the reply
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do we need to terminate the threads created ?as the thread count is increasing and taking the cpu as well the cpu is not coming down untill server is restarted .


The thread should terminate when it is complete. It looks like from the source code that it should terminate as soon as the stream is closed. I would suggest that you debug it to confirm that the stream is actually closing.

In terms of resources, I would not worry about the threads. You will run out of file/network descriptors *way* before you have a thread problem. You need to confirm that the streams are closing.

Henry
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in class StreamGobbler i closed streams with

br.close();
isr.close();

still i am getting the same error
thanks for the replies
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try calling enumerate on the thread group and getting a list of the active threads, and printing their names.
[ October 08, 2005: Message edited by: Mr. C Lamont Gilbert ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this has anything to do with the price of beans, but in one of my toy programs the StreamGobblers ran for some time after waitFor() came back. I'd add join() on the two gobblers right after waitFor() just to see what happens.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I don't know if this has anything to do with the price of beans, but in one of my toy programs the StreamGobblers ran for some time after waitFor() came back. I'd add join() on the two gobblers right after waitFor() just to see what happens.


I actually encountered something similar in one of projects (wished it was a toy program though). The problem was caused by the fact that the program that I started, started another program. So even though the program that I started finished, meaning the waitFor() method returns, the streams were not closed because for some reason, I inherited the streams from the child process.

In the case that was Unix, I just started the process as a cron job, letting the cron server inherited the streams. And in the case of Windows, I had to live with it. Sooner of later the child process ended, and the threads terminated.

Henry
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fallowing is the out put i am getting while i am enumerating the activeCount() of the thread .
what is the main thread indicate is it the thread i started ?




thanks for the reply
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one tell me what is thread main every time call the method its increamenting ?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
next step is to name your threads


The naming increment is not entirely 'thread safe' but were just testing here.
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help
Thread[Gobbler 8,5,main]
Thread[Gobbler 9,5,main]

after each submit i am getting two threads old threads are not there in the out put but the main threads are increasing in number?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!