Hi Corey McGlone, Barry Gaunt, Jason Menard, Dirk Schreckmann,
Long time. (am still working on my EJB certs). But wanted to ask this.
This write-up is to check if my understanding of threads is ok or not. I invite opinions from you. How correct is this? I was just trying to imagine an understanding of threads. I hope my understanding is not imaginary.
This discussion is not about code just the concepts. Correct me whereever I am wrong.
Edited by Corey McGlone: Broke up long lines of code to preserve page formatting. [ December 02, 2004: Message edited by: Corey McGlone ]
First of all, unless you're doing a counted loop, why not use a "while" loop. You "for" statements would be a lot clearer written like this:
In addition, rather than having the waiting thread "wake up" every so often to check on the flag, why not just have it "wait"? You can use the metod "wait", which is defined in Object, to cause the waiting thread to go into a dormant state. Then, when your other thread is ready to active that waiting thread, you can call notifyAll().
I'd still suggest keeping your check in a while loop, like this:
That way, if something else were to notify this thread, it would still check the flag to make sure it was ready to process. If not, it would simply go back to waiting.
Not only is this a more efficient solution, but it's also easier to understand, in my opinion.
But wait, notify, are about locks. and u can call wait only on an object that is the lock used for synchronization.
Hey read this a bit more slowly. I wish to simplify understanding of threads. I dont want wait and notify to comlicate things in this discussion.
I am trying to show how sleep is a more efficient way than a do nothing loop and how join is an extension of a loop around a sleep and a flag conditional.
I am trying to make a article on threads for a ppt I have to present in office to some visting college students. I am trying to demystify thread programming for them. I will ask about wait, notify, synchronize later.
I thouht people understand simple loops without any sleep statements more easily. From there onwards I am slowly bringing in the concepts of sleep, join. Also for now I am trying to keep it simple and will bring in the exceptions later.
What do u think of the main post? I am off to bed. Hope you give your reply. Bye. Thanks. [ December 02, 2004: Message edited by: Swamy Nathan ]