aspose file tools*
The moose likes Threads and Synchronization and the fly likes , how many threads will be created?. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark ", how many threads will be created?." Watch ", how many threads will be created?." New topic
Author

, how many threads will be created?.

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
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 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
Anirvan Majumdar wrote: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 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: 3775

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Anirvan Majumdar
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: 3971
    
  17

The original question was this:


Wednesday, September 02, 2009 01:25:21 Subject: , how many threads will be created?.
I have written a java program,

Class 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
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 realize that (AWT or Swing applications, Timers, ExecutorSurvices, etc...).


1) if a class interacting with many classes and interaction with any remote resource,then single thread take care all of this


It could. Number of classes and method calls have nothing to do with threads. Remote resources have nothing directly to do with Threads, though some implementations may use Threads to make access more efficient.


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 tell us where you got that code from, and in what context? The code itself is a little problematic because there is no static variable in Thread called currentThread. There is a static method called currentThread() though. When asking questions it helps to be precise. If you could show us the code source or copy/paste the code exactly then we would be able to answer the question more precisely.

That code is not creating a Thread though. It appears to be using the value returned from currentThread() as a key to a map, in order to get some properties. The goal here is probably that the hash table is capable of storing different properties for different Threads (Thread-local values). If you had one object which may be used by multiple Threads and you wanted different properties stored for each possible Thread then this is one solution. Personally, I would not use this myself. Instead I would use the ThreadLocal object because the intent would be more clear.


Steve
 
 
subject: , how many threads will be created?.
 
Similar Threads
getting number of threads
Number of Threads
value changed for no reason
return & system.exit()
Servlet Container