File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Advice on simple threadding... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Advice on simple threadding..." Watch "Advice on simple threadding..." New topic
Author

Advice on simple threadding...

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Hi!
I am working on a Logger. The system is simple. You have loggers - Console, file, Html, etc. You have log managers - SimpleLogManager, ThreadedLogManager, TimedLogManager. Here's a code snippet:

Everything works fine... but I am not sure how fine it works...
I was working on ThreadedLogManager. What I want it to do is to handle all the logging in a background thread. So here's what my ThreadedLogManager looks like so far:

It seems to log just fine, but whenever my main() is finished executing, I still see my proccess running in my IDE's (NetBeans 3.5) execution window... And if I try to do System.exit(0); at the end of my main, nothing gets logged at all (not unless I do Thread.sleep(1000) before exiting)...
So can you tell me what my problem is? And give some advice perhaps?
Thank you in advance,
Ivan Jouikov.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Well, I see a few problems with your logger. First, you have an infinite loop in your run() method which you probably break by issuing the interrupt(). This is not a recommended way to terminate the threads. Rather, have a boolean that you can set to indicate that the thread should finish.
The other problem is that you have a busy loop in your run() that will waste a lot of CPU cycles. The standard solution here is to use the wait()-notify() mechanism. That is, when a new message arrives, you notify() the logger, and after the message is logged, the logger should wait().
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
I thank thee, oh mighty wizard!
I shalt try that and report shortly with the results!
Again thank thee!
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Ok I made some modificiations.. here's what it looks like now:

Whenever I run it, the first message gets logged properly, and right after that (on the second message?) I get the following:


... and my thread seems to keep on running... can u tell me where my problem is?
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
OK after some reading and thinking, here's what I came up with:

Since I am new to threading, I will try to explain what I think of this, and can you please correct me if I am wrong?
Ok, so what happens when this class gets first constructed is:
- we create a background thread with minimal priority and run it...
- It immediately jumps into the run() method
- There it creates a monitor for the messages queue (synchronized(messages))
- Since !done == true, it runs through the code until it reaches wait() line ( since everything is empty )
- it puts a wait() on messages monitor... so the writeThread gets "suspended" (1.CORRECT?)

Ok whenever we want to log something ( log() ):
- first we reach the monitor ( synchronized(messages) ), and we will wait until the background thread is finished logging. ( 2.Is this efficient btw? won't that create a bottleneck which will make the whole threading useless? Or will it work since writeThread has lower priority? )
- We'll put the level of logging and the data into the queue.
- we'll notify the messages monitor that it is ready to log ( 3. CORRECT? )
Ok now once we notified it, the messages.wait() in run() should return and our loop will continue:
- we make sure that queue is not empty ( 4.would it be OK to have if() instead of while()? or does while() provide more "safety"?)
- we'll retrieve data from the queue and dispatch it to each individual logger

do I sound correct? Because whenever my program is finished running, NetBeans still shows it in the porccess list...
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
do I sound correct? Because whenever my program is finished running, NetBeans still shows it in the porccess list...
You still have an infinite loop, because the only way to terminate it would be to either call the interrupt() (which you probably don't call anymore) or setting the boolean "done". That boolean, however, is set in the finalize() method that is not guaranteed to execute. You should never rely on the finalize().
The standard way to handle the thread termination it to declare your boolean as volatile, and have a method that can set it, whenever you want to terminate the thread gracefully.
I'll get to your other question later (unless someone else steps in).
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Threads and Synchronization forum...
[ September 13, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Advice on simple threadding...