You are on page 1of 4

6/13/13

, how many threads will be created?. (Threads forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author

, how many threads will be created?.


posted 9/2/2009 10:55:21 AM

jacob deiter Ranch Hand Joined: Apr 02, 2008 Posts: 576

I have written a java program, Class A{ //My code here }

For executing the above program, how many threads will be created?. I welcome your detailed explanation
Anirvan Majumdar Ranch Hand Joined: Feb 22, 2005 Posts: 261

posted 9/2/2009 3:55:10 PM

In your case, only one main thread will be created - assuming you're not going to instantiate any 'thread' classes from Class A., and there's a main() defined in the class. Typically, when a Java application is executed, the JVM [java] process gets
1/4

www.coderanch.com/t/460814/threads/java/threads-created

6/13/13

, how many threads will be created?. (Threads forum at JavaRanch)

loaded. This in turn spawns the main thread to execute your class. You might wonder if you instantiated another class from the first one, whether another thread will get spawned? The answer's no. Unless of course, that other class is a thread implementation itself.

jacob deiter Ranch Hand Joined: Apr 02, 2008 Posts: 576

posted 9/3/2009 9:42:27 AM


A nirvan Majumdar wrote:

In your case, only one main thread will be created - assuming you're not going to instantiate any 'thread' classes from C lass A., and there's a main() defined in the class. Typically, when a Java application is executed, the JVM [java] process gets loaded. This in turn spawns the main thread to execute your class. You might wonder if you instantiated another class from the first one, whether another thread will get spawned? The answer's no. Unless of course, that other class is a thread implementation itself.

1) if a class interacting with many classes and interaction with any remote resource,then single thread take care all of this 2) I read java code in that for getting enviroment data ,they are using hashhastble and thread as follow,.get Properties properties =some_hash_table.get(Thread.currentThread) could you please explain why? 3) as you said in your reply ,single process will b created for a application
Vijitha Kumara Bartender Joined: Mar 24, 2008 Posts: 3673
I like...

posted 9/3/2009 11:19:51 AM


jacob deiter wrote:

... 2) I read java code in that for getting enviroment data ,they are using hashhastble and thread as follow,.get Properties properties =some_hash_table.get(Thread.currentThread) could you please explain why?

Can you show that code snippet ? (Hashtable's get(java.lang.Object) )

SC JP 5 | SC WC D 5 [How to ask questions] [Twitter]

Anirvan Majumdar

posted 9/3/2009 11:47:47 AM


2/4

www.coderanch.com/t/460814/threads/java/threads-created

6/13/13

, how many threads will be created?. (Threads forum at JavaRanch)

Ranch Hand Joined: Feb 22, 2005 Posts: 261


jacob deiter wrote:

.... 2) I read java code in that for getting enviroment data ,they are using hashhastble and thread as follow,.get Properties properties =some_hash_table.get(Thread.currentThread) could you please explain why? ....

That looks like a strange bit of code, because I haven't typically seen anyone mapping values to a Thread instance. After all, every time the control comes to this statement, it will have a different thread instance [and hence, a different hash value for the instance]. My fair guess will be that in your application there's some sort of a thread pool being maintained. And each instance in that pool is associated with some unique set of properties. As mentioned before, if you can share the code snippet it might be helpful. I'd also suggest you investigate a bit more to understand whether or not your application has a multi-threaded implementation.

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3080

posted 9/4/2009 6:01:18 PM

The original question was this:

5
I like...

Wednesday, September 02, 2009 01:25:21 Subject: , how many threads will be created?. I have written a java program, C lass A{ //My code here }

For executing the above program, how many threads will be created?.

The answer to that is: It is impossible to say. The answer depends on what 'My Code' is. If you do nothing that generates new Threads then the answer would be at least one but usually 2, maybe more depending on JVM configurations. The two minimums would be 1) the main thread for running the application
www.coderanch.com/t/460814/threads/java/threads-created 3/4

6/13/13

, how many threads will be created?. (Threads forum at JavaRanch)

2) a thread for garbage collection timing There are a lot of cases where more threads would be created but for which would be invisible to you, and other cases where threads will be made and you should rea

www.coderanch.com/t/460814/threads/java/threads-created

4/4