• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and Synchronization

 
jesse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering what would happen if:
I have a thread that has a lock on a synchronized method. This thread never dies (is in a continual while loop)and is only put to sleep. So as i understand it this thread should never give up its lock. If this thread never gives up its lock on the synchronized method what will happen if i continue to create new threads. can there be too many threads in a ready state? how long will it take to crash???
if someone can help me out i would really appreciate it!!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a test, I once wrote a program that created several thousand threads - they didn't do much and didn't take up much memeory, though. The number of threads you can create seems to be limited only by available memory. Each thread gets a certain amount of stack space (256K ?) and that's its only memory requirement, as far as I can tell. Any objects it creates are allocated from the shared heap. Oh yeah, there's always <code>ThreadLocal</code>, but let's not go into that...
So your thread has a lock on some object, and spends a lot of time asleep. If you create more threads, they'll transition back and forth between ready and running states, unless they get put to sleep or encounter synchronized code. If they try to lock the same object that the original thread is using, they'll block at that point until the original thread gives it up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic