File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Corey, Barry , Dirk- have a look please (Threads) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Corey, Barry , Dirk- have a look please (Threads)" Watch "Corey, Barry , Dirk- have a look please (Threads)" New topic

Corey, Barry , Dirk- have a look please (Threads)

Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
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 ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.

SCJP Tipline, etc.
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
granted about the while.

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.
[ December 02, 2004: Message edited by: Swamy Nathan ]
I agree. Here's the link:
subject: Corey, Barry , Dirk- have a look please (Threads)
jQuery in Action, 3rd edition