I have several threads. And I’d like to create sub-threads in one of them. So I’d like know whether java thread has a tree structure. Or the new created sub-threads is just the sibling of other threads. And What’s the resource allocation strategy when these thread are competing for resources? Does the parent thread has higher priority ?
Thanks.
Jeff
There’s no thread hierarchy, all threads are siblings of each other. There is no concept of “parent thread” or “child thread”. You’ll have to be more specific about what you mean by resource allocation strategy — what resources are you referring to?
If it’s memory (by far the most common type of resource), then the answer is that the memory allocator is thread safe: multiple simultaneous allocations happening on different threads will always work correctly. If you run out of memory, then some unlucky thread is going to get an
OutOfMemoryError.If you’re referring to other types of resources, then it depends entirely on the implementation of the resource. It’s either going to be thread-safe or non-thread-safe. If it’s thread-safe, you can allocate the resources freely from multiple threads. If it’s not thread-safe, then read the documentation for that type of resource.