JaVa
   

What Is a Thread?

A thread is simply a running process of execution. The simplest way to explain a thread is to tell you that when an app begins by invoking the main method, the code that is executed is running in a thread. This thread is started by the Java Virtual Machine and enters the main method to begin flowing through your code. Tracing the path of what we might call the main thread is relatively simple, as code is executed line by line starting in main; although invoking methods and making loops can complicate the path of execution, we are still in relative control of the flow of the program. However, I use the term "program" loosely because the main thread may not be the only thread running in our program. Yes, that is correct; along with our main thread, there can be other threads running at the same time (although they cannot actually run at the exact same time as one another, except perhaps on a multiprocessor machine, when they can take equal processor time slices), which can complicate things immensely and slow down the code if handled badly. For games programming, you should use as few threads as possible (using one main thread to handle the running of the game). In Java we don't quite have that luxury, as some important aspects of games coding in Java rely on handling multiple threads. The most important "other thread" is called the Event Dispatch Thread. The Event Dispatch Thread is what gives a programmer his power. It's an energy field created by all living things. It surrounds us and penetrates us. It binds the galaxy together.

Let me rephrase that a little. The Event Dispatch Thread is a thread that handles events in your app. An event in the case of the Event Dispatch Thread could be from the mouse (e.g., mouse pressed), the keyboard (e.g., key pressed), or even a window event (e.g., a window closing event). The basic idea of the Event Dispatch Thread is that it will receive events from other, hidden threads, such as window, mouse, and keyboard events and then dispatch those events to your app, provided you specify that you want to listen for those events. We will look at the Event Dispatch Thread in much more detail in s 9 and 10. For now, it is important to understand how to create and use our own threads before we get caught up in someone else's threads; besides, we haven't mentioned synchronization yet.

JaVa
   
Comments