This tutorial covers how to create and manage threads in Java's multithreaded environment.
Among the defining characteristics of Java is its built-in support for multithreaded programming. This support, which has been present in Java from the start, is provided by the Thread class, the Runnable interface, several methods supplied by Object, and the synchronized keyword.
Multithreading enables you to write programs that contain two or more separate paths of execution that can execute concurrently. Each path of execution is called a thread. Through the careful use of multithreading, you can create programs that make efficient use of system resources and maintain a responsive user interface.
Multithreading Fundamentals : At its core, multithreading is a form of multitasking. There are two distinct types of multitasking: process-based and thread-based.
It is important to differentiate between the two. As it relates to this discussion, a process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example, it is process-based multitasking that allows you to download a file at the same time you are compiling a program or sorting a database . In process-based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.
In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. Because a program can contain more than one thread, a single program can use multiple threads to perform two or more tasks at once. For instance, a browser can begin rendering a Web page while it is still downloading the remainder of the page. This is possible because each action is performed by a separate thread.
Although Java programs make use of process based multitasking environments, process-based multitasking is not under the direct control of Java. Multithreaded multitasking is. All processes have at least one thread of execution, which is called the main thread, because it is the one that is executed when a program begins. From the main thread, you can create other threads. These other threads can also create threads, and so on. Multithreading is important to Java for two main reasons. First, multithreading enables you to write very efficient programs because it lets you utilize the idle time that is present in most programs. Most I/O devices, whether they be network ports, disk drives, or the keyboard, are much slower than the CPU. Thus, a program will often spend a majority of its execution time waiting to send or receive information to or from a device. By using multithreading, your program can execute another task during this idle time.
The second reason that multithreading is important to Java relates to Java’s eventhandling model. A program (such as an applet) must respond quickly to an event and then return. An event handler must not retain control of the CPU.
Check out the video link below.