[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]
[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570018020898{background-color: #f6f6f6 !important;}”]
The so called Threads in Java handles the order of priority for how a program should executes the code.
[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
Most programs execute using only one thread that handles the priority order for how the program should execute. But what if multiple operations need to be performed exactly at the same time? By creating several threads that run in parallel, this can be made possible. This functionality is useful in more extensive programs (for example, in a multiplayer game that needs to handle multiple inputs simultaneously).
A thread in Java can be declared in two different ways,
We will, of course, look at how we do for both of them. But first, there is a difference between them that are good to know,
In our game, we will only need one thread. We have chosen to use the Runnable interface in our game and is what we usually use when working with Threads.[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
We could view it as Threads enables multitasking. We use threads in Java to make the program faster by doing several things at once. Furthermore, additional uses could be when we want to treat multiple users at the same time. For example, a program that will take care of customers on a website. When shopping online, you do not want to wait in a long queue for others to finish before you can complete your order.
Finally, by enabling multitasking, we can respond more quickly to the information a user provides to the program. For example, if we have a GUI to draw something based on what the user is doing. If this happens sequentially and the program must wait all the time until the user is completely clear, and then the user must wait until the GUI finishes with the operation. If we use threads in Java, we can execute it in parallel and perform each of the tasks running side by side.[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
We create a thread by inheriting (extends) the Thred class where the run() method is defined.
The syntax for declaring a Thread by inheriting the class is,
import java.lang.*; // Inherit Thread public class Counter extends Thread { // Declare a new Thread Thread T; // The order in which the program should be executed public void run() { .... } }
[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
In a similar way, a thread can be created by implementing (implements)Runnable where the run() method is defined.
The Syntax to create a Thread in Java with Runnable is:
import java.lang.*; // Implements Runnable public class Counter implements Runnable { // Creates a new thread Thread T; // The order in which the program should be executed public void run() { .... } }
[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
There are a couple of different states a thread can have,
The image below shows examples of how we can use some of the different methods available to create a thread. Note how we have different states and can use the methods to customize the state of our threads.[/vc_column_text][vc_empty_space height=”50px”][vc_single_image image=”20100″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][vc_empty_space height=”40px”][vc_column_text]
[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
For a complete list of methods for the Runnable interface see the Oracle website[/vc_column_text][vc_empty_space height=”40px”][vc_column_text]
[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]
[/vc_column_text][vc_empty_space][/vc_column][/vc_row]