Every now and then I end up looking at ways to improve our use and handling of the various threads that are used internally by JPedal. With this in mind I decided that I would write an article covering useful practices for java threads that should become second nature to all who have to deal with threads on a regular basis.
Daemon threads
Daemon threads can be thought of as background threads. They sit in the background working away at a task whilst the program is still running. When I refer the the program I mean there is a non-daemon thread currently running as when all non-daemon threads are running the program will close. Should this happen then all daemon threads will be halted, anything being executed will “drop out” this means that the finally block will not even be executed so it is important to ensure that daemon thread do not contain anything that will cause harm if the execution is just ended without warning.
Personally by default I set all my threads to run as daemon threads when writing them. The majority of the time, when adding a thread, it can be added as a daemon thread and ran in the background. Not until I have the base work of a Runnable object ready for the thread do I decide if I should change it from a daemon thread.
A thread can be made a daemon thread using the following method.
Thread.setDaemon(boolean on);
Naming your threads
The second thing I do after making my thread a daemon thread is to ensure the thread has a name. Technically this is not required but can become very useful, especially if or should I say when you have to debug any code that interacts with the thread.
By setting a name for the thread you can use the method Thread.getName(); to return a String containing the name of the thread. More importantly, when using an IDE’s debug mode or monitoring the JVMs threads in some other way, the thread should be displayed with the set name displayed there. This will make debugging issues with involving one of your thread much easier to identify and resolve, especially if there are multiple threads running and your not sure which is which.
A thread can have its name set using the following method.
Thread.setName(String name);
The Event Thread
The event dispatch thread is used to execute swing object methods. This thread is created for you when starting an application that uses swing. The reason all swing object methods are executed on this thread is because swing is not entirely thread safe and can cause different issues when non-thread safe code is executed on different threads.
For this reason it is important to make sure nothing that uses these non-thread safe methods is executed on any thread other than the event thread. One way this can be done is using the SwingUtilities methods in the following manner.
if(!SwingUtilities.isEventDispatchThread()){
Runnable setTextRun = new Runnable(){
@Override
public void run(){
//Execute Code
}
};
SwingUtilities.invokeLater(setTextRun);
}else{
//Execute Code
}
If you have any other ideas or practices that you find useful when handling threads feel free to leave them as comments.
Are you a Developer working with PDF files?
Our developers guide contains a large number of technical posts to help you understand the PDF file Format.
Do you need to solve any of these problems?
Display PDF documents in a Web app |
Use PDF Forms in a web browser |
Convert PDF Documents to an image |
Work with PDF Documents in Java |