by Dinesh Thakur Category: Multithreading

Java has two types of threads : user thread and daemon thread.

A user thread is a thread that will run on its own independently of other threads. In other words, a user thread has life of its own that is not dependent on the thread that creates it. It can continue execution, even after the end of the thread that created it. One example of user thread is the main thread which runs your main () thread. On the other hand, a daemon thread is subordinate to a user thread i.e. a daemon thread will automatically terminate when no more user threads are running in the application.

 
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

When a large number of threads are created, degrades the performance of your application. So Java provides a technique of thread pooling to solve this problem.

The thread pooling allows you to reuse a single thread repeatedly instead of creating a new thread for each task and allowing it to be destroyed when the task completes. It avoids the overhead associated with creating a new thread by maintaining a pool of available threads and retrieving one from the pool when necessary.

 
by Dinesh Thakur Category: Multithreading

In order to apply locks explicitly, an instance of the Lock interface needs to be created which is defined in the java.util.concurrent. Locks package. Using the Lock interface is similar to using the synchronized keyword. The Lock interface provides lock () and unlock () methods to acquire and release the lock respectively.



 
by Dinesh Thakur Category: Multithreading

Method level synchronization prevents two threads from executing method on an object at the same time. A method can be synchronized by using the synchronized keyword as a modifier in the method declaration. Its syntax is a follows,

 
by Dinesh Thakur Category: Multithreading

final void join () - The isAlive () method occasionally useful as continuously checking the value returned by this method and then sleeping makes a very inefficient use of the CPU time. So a better alternative to wait for a thread to die is to use join () method of the Thread class.

 
by Dinesh Thakur Category: Multithreading

In order to make sure that the main thread finishes last, we introduced a long enough delay by calling the sleep () method within the main() method so that all other threads finish before the main thread. But this of course is hardly a satisfactory solution because if your estimate for the time delay is too short then other threads could finish after the main thread finishes. Therefore, the Thread class provide method isAli ve ()  to determine if a thread has ended .

 
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

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

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.

 
by Dinesh Thakur Category: Multithreading

Thread is a lightweight process. It is lightweight because they utilize the minimum resources of the system. Thread is an independent concurrent path of execution of a group of statements. Thread takes less memory and less process time.

 
by Dinesh Thakur Category: Multithreading

If we talk about the mechanism of synchronization, then as one thread exits from the monitor then it must inform the waiting threads that it has left the monitor, now suspended thread can proceed to acquire the lock on the resources or entered in the monitor. If that is not possible then the waiting thread will always be in the waiting list. So, to solve this problem threads must communicate with each other.

 
by Dinesh Thakur Category: Multithreading

 Priority allow the scheduler to take the decision when the thread should be allowed to run, and in which order. The higher priority threads get more CPU time then lower priority threads. A lower priority thread can be preempted by higher priority thread. The threads having equal priority get equal CPU time.

 
by Dinesh Thakur Category: Multithreading

The ability of the Operating system to execute several programs simultaneously is known as multitasking. In system terminology, it is is a powerful programming tool that makes it possible to achieve concurrent execution of multiple units of a program called multithreading. In multithreading,  the application (process) is divided into two or more subprograms (processes), Several such processes originating from a single task, can be simultaneously started and handled by Java, which can be implemented at the same time in parallel. The processor is doing only one thing at a time, but it switches between the processes so fast that it appears to human beings that all of them are being done simultaneously. This mechanism of treating a single task as several independent processes simultaneously is called multithreading. Each separate process is called a thread. Each thread is executed one at a time in the CPU. Multithreading enables a program to do more than one task at a time and also to synchronize these tasks.  

 
by Dinesh Thakur Category: Multithreading

By implementing the Runnable interface the thread creation is the easiest way. You can construct a thread on any type of object that implements Runnable. To implement Runnable interface, a new class need only to implement run() method. When you implement the Runnable interface then create an instance of Thread from within that class. The Thread class has several constructor.

 
by Dinesh Thakur Category: Multithreading

In Java, a user defined thread can be created to implement the Runnable interface. The Runnable interface has only one method run() that you must have to implement in your class, that you want to create as thread. The run() is the method in which that code is written, which is counted as thread task.

 
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

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.

 

Page 2 of 2



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, 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