by Dinesh Thakur Category: Multithreading

The Java language and its run-time system was designed keeping in mind about multithreading. The run-time system depend upon multithreading. Java provides asynchronous thread environment, this helps to increase the utilization of CPU.

 
by Dinesh Thakur Category: Multithreading

Every thread in Java has a priority that helps the thread scheduler to determine the order in which threads scheduled. The threads with higher priority will usually run before and more frequently than lower priority threads. By default, all the threads had the same priority, i.e., they regarded as being equally distinguished by the scheduler, when a thread created it inherits its priority from the thread that created it. However, you can explicitly set a thread's priority at any time after its creation by calling its setPriority() method. This method accepts an argument of type int that defines the new priority of the thread. Its syntax is.

 
by Dinesh Thakur Category: Multithreading

Another way to create a thread in Java is to define a class that implements the Runnable interface. The previous technique of creating threads by extending the Thread class does not work when you want a class that extends another class and can also run as a thread. It is because java supports only single inheritance, i.e., does not allow a class to extend more than one class. So in such a case implement the Runnable interface. The Runnable interface declares only one method run () that contains the code executed when the thread started. Therefore, if you define a class that implements the Runnable interface, then it must implement the run () method.



 
by Dinesh Thakur Category: Multithreading

A thread can undergo some states during its life cycle. It is because in a multithreaded environment when multiple threads are executing only one thread can use the CPU at a time, and all other threads should be in some other states either waiting for their turn for the CPU or waiting for some other condition to be satisfied.

A thread is always in one of five states: newborn, runnable, running, dead and blocked. Figure shows the life cycle of a thread.

 
by Dinesh Thakur Category: Multithreading

In this Example, we shows how the constructors and methods of the Thread class are used. Here, we have created a subclass MyThread that extends Thread class. This class consists of a field which stores the time in milliseconds for which the thread will sleep. It also contains a parameterized constructor that contains two parameters str of String type and d of int type. This constructor calls the superclass constructor that sets the Thread's name to the value passed in str and delay field is set to value passed in d.

 
by Dinesh Thakur Category: Multithreading

Another method to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. We can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run().

 
by Dinesh Thakur Category: Multithreading

As any Java program comes under execution, first thread starts immediately, called the main thread. There are two importance's of main thread as follows:

 
by Dinesh Thakur Category: Multithreading

Java Thread: One of the dominant features of the Java language is that it provides built-in support for multithreading - the concurrent running of multiple threads within the same program. Creating a thread in Java is relatively easy. Unlike the old fashioned programming languages, where you have to invoke system-dependent procedures and functions to implement multithreading, in Java, it is no harder than creating an instance of other classes.

The programs that we have written so far had only one entry point (main() method) and one exit point. All the instructions in these programs executed serially one at a time beginning at the first statement in main () and continuing sequentially through the statements until the program ends. This single sequence of executable statements within a program is known as a thread. In other words, a thread is s flow of execution of a task in a program, so it is known as a thread of execution or a thread of control.

 

Page 1 of 5



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles