• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

, how many threads will be created?.

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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) )
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic