Threads, in general, allow a program to behave more efficiently by passing off parts of a process so that multiple items can be done simultaneously. With the advent and growth of multi-core processors which allow for many natural threads, this has been increased.
Why do we need threads?
Well simply put, there is always something that is slow in a computer that we are waiting on.
Normally, it is an IO type request. Let’s say a user is typing in a word processing document, or a user form field. The typing is slow, so a thread can go off behind the scenes and do things between those key strokes. This can include auto saving a document, running spelling and grammar checking, looking up items in a thesaurus, words counts, finding links to other relevant documents, etc. In fact, you can start multiple threads, that each do one or more of these items all while the user is slowly typing a mere 35-75 words per minute.
Of course, we can find other examples, such as one thread playing a video, while another keeps downloading the next section so there is no interruption. Or a game engine having different threads for Graphics, Audio, Opponent AI, and even networking with a server for additional users to play with you. There may even be threads which sub-divide those processes even more, if they are computationally expensive.
Java and Threads
While multi-threading is sometimes an add-on for a language, it is built into Java from the beginning. In fact, there are two ways to create a thread in Java.
First, you can extend the Thread
class.
public class ThreadExample extends Thread {
public void run() {
System.out.println("This code is running in a thread");
}
}
While the first process works great if you are not already extending another class, like we did when we made an GUI class which extended the JPanel. In that case, you need to implement Runnable
.
public class ThreadExample extends NonThreadedClass implements Runnable {
public void run() {
System.out.println("This code is running in a thread");
}
}
Notice, how in both of their cases, you need to create a function called run. We’ll talk about that more in a little bit when we dive deeper into the code.
Problems with Threads
There are two potential problems you will run into with multi-threaded applications.
First, is concurrency. That is, because threads are running at the same time, you can run into an issue where you don’t necessarily know which order code is being run in. This is a problem if variables are shared between threads.
With potential different values for each time the application is run, might create some unexpected behaviors. Therefore, you should look to avoid sharing data between threads where possible. If you do need to share data you want to look at locking data or using a flag to verify a section of code has run before another.
The second issue is one of locking. As part of the concurrency fix, you might choose to have a lock set. However, if you lock a variable, and don’t release it, then your whole system can be locked without a way to unlock.
A third potential issue is that Exception handling is even more important. Having an exception be thrown, can cause a serious issue if it occurs in a thread. As you will probably need to handle it and restart your thread, or the process that caused it.
Finally, Threads are expensive., time wise that is. While using threads will save you execution time by allowing you to split up the tasks (assuming you find good ways to split up the task), they each take time to spin up and get ready. The more your processor allows for threads, the better, but many processors (ahem Intel processors) only allow 2 threads per core. So you still have to spin the thread up, and then task switch if you have a few. So for short apps, this doesn’t seem to make a lot of sense, and can even actually slow you down. However, in larger tasks it will save you time in the long run, making your apps much more efficient.
Intro to Java Threads was originally found on Access 2 Learn
2 Comments
Comments are closed.