Here is the listing of Java programming examples on Multithreading.
1. Java Examples on Synchronization
Synchronization is the capability to control the access of multiple threads to any shared resource. Synchronized keyword is all about different threads reading and writing to the same variables, objects and resources. Synchronized method is used to lock an object for any shared resource. Synchronized block can be used to perform synchronization on any specific resource of the method. Scope of synchronized block is smaller than the method. This section contains Java programs on synchronization, method and block level synchronization, run() method as synchronized, solving producer consumer problem using synchronization, checking synchronized block is executed by only one thread or not.
Java Program to Use Method Level Synchronization
Java Program to Use Block Level Sychronization
Java Program to Check Whether Define run() Method as Synchronized
Java Program to Solve Producer Consumer Problem Using Synchronization
Java Program to Show that Method Will be Verified Whether it is Synchronized or Not
Java Program to Show How Can Class Object be Locked Using Method Level Synchronization
Java Program to Synchronize the Threads Acting on the Same Object. The Synchronized Block in the Program can be Executed by Only One Thread at a Time
2. Java Examples on Deadlock
Deadlock occurs in a situation where two or more threads are blocked forever, waiting for each other. Deadlock can occur when multiple threads need the same locks, at the same time, but obtain them in different order. Deadlock are mainly occur because of lock acquisition in wrong order. The Java programs in this section to solve deadlock problem using thread, avoiding dead locks, showing and depict the situation in which deadlock can occurs.
Java Program to Avoid Dead Locks
Java Program to Solve Deadlock Using Thread
Java Program to Show a Situation in Which a Deadlock can Occur
Java Prgroam to Depict a Situation in Which a Deadlock Can Occur
3. Java Examples on Runnable Interface and its Implementation
Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. To create a new thread, program will either extend Thread or implement the Runnable interface. Run method is used for Entry point for the thread. This section contains Java programs on runnable interface, creating a thread that implements runnable interface, checking whether given class can create new runnable method and checking given class can runnable or not.
Java Program to Check If a Given Class is Create New Runnable Method
Java Program to Check if Given an Anonymous Class is Runnable
Java Program to Show Whether Checking Can You Start Runnable Method
Java Program to Use of Runnable Interface
Java Program to Create a Thread that Implement the Runable Interface
4. Java Examples on Thread Priorities
Thread priorities are used by the thread scheduler to decide when each thread should be allowed to run. Higher priority threads get more CPU time than lower priority threads. To set a threads priority, use the setPriority( ) method, which is a member of Thread. Value of level must be within the range MIN_PRIORITY and MAX_PRIORITY. Currently, these values range are 1 to 10. NORM_PRIORITY is used to specify the default priority. This section contains Java programs on displaying the threads priority, checking priority level of a thread, setting the thread priority, accessing the priority using thread object.
Java Program to Show the Priority in Threads
Java Program to Check Priority Level of a Thread
Java Program to Set the Priority of a Thread
Java Program to Get the Priorities of Running Threads
Java Program to Access the Priority You Can Use Method With Thread Object
Java Program to Understand the Thread Priorities. The Thread with Higher Priority Number will Complete its Execution First
5. Java Examples on Threads & Methods
The join() method is used to hold the execution of currently running thread until the specified thread is dead. Thread class provide constructors and methods to create and perform operations on a thread. The easiest way to create a thread is to create a class that implements the Runnable interface. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread. Static blocks are used when a block of code needs to be executed during loading of the class by JVM. static methods can be called without creating an object of the class. This section contains java programs on join thread, defining thread by using extending thread, handling IllegalThreadStateException, checking whether static block can be used or not, understanding how threads can be used to animate the things and checking whether we can execute java application without using main method or not.
Java Program to Use Join Thread
Java Program Defining Thread By Extending Thread
Java Program to Handle IllegalThreadStateException
Java Program to Check Whether Static Block will be Used
Java Program to Show Why Exit Method is Used in Static Method
Java Program to Check Whether Define the run() Method as Abstruct
Java Program to Check if Other Overloaded Method will not be Invoked
Java Program to Understand How Threads Can be Used to Animate the Things
Java Program to Check If Whether Execute Java Application Without Having Main Method
6. Java Examples on Thread Class
Thread class defines several methods that help manage threads. setName( ) is used to change the internal name of the thread. Destroy() is used to destroy the thread. suspend() puts a thread in the suspended state and can be resumed using resume() method. currentThread() method returns a reference to the currently executing thread object. getPriority() is used to obtain the current priority. Alive( ) method returns true if the thread upon which it is called is still running. This section contain programs on thread examples like setname, Destroy(), suspend(), currentThread(), run(), getThreadGroup(), getPriority(), Alive() and getName(). It also contains program to check given thread is alive or not, getting the name of running thread and thread.
Java Program to Illustrate Thread Example for setName(string name)
Java Program to Illustrate Thread Example for Destroy()
Java Program to Illustrate Thread Example for suspend()
Java Program to Illustrate Thread Example for currentThread()
Java Program to Illustrate Thread Example for run()
Java Program to Illustrate Thread Example for getThreadGroup()
Java Program to Illustrate Thread Example for getPriority()
Java Program to Illustrate Thread Example for Alive()
Java Program to Illustrate Thread Example for getName()
Java Program to Show Interfaces Can be Extended
Java Program to Check a Thread is Alive or Not
Java Program to Get the Name of a Running Thread
Java Program to Get the Name of the Thread
Java Program to Check if a Given run() Method is Overloaded in the Thread Class
Java Program to Check Whether Define a Thread Class Without Defining run() Method in the Class
7. Java Examples on Suspending, Resuming & Stopping Threads
Stop( ) is used to stops the thread. suspend() puts a thread in the suspended state and can be resumed using resume() method. wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. Sleep() method of Thread class is used to sleep a thread for the specified time. This section contains java programs on stopping a thread, and checking whether its stopped or not, suspending a thread for sometime, terminating the thread by pressing enter button, use of wait() and sleep() method.
Java Program to Stop a Thread
Java Program to Suspend a Thread for a While
Java Program to Check a Thread has Stop or Not
Java Program to Use of Wait and Sleep Method
Java Program to Terminate the Thread By Pressing the Enter Button
Java Program to Illustrate Thread Example Program for Stop(Throwable obj)
8. Java Examples on Creating Multiple Threads
There are two ways to create multiple threads they are by extending Thread class and implementing Runnable interface. Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface. currentThread() returns the reference of currently executing thread. This section contains programs on execution of multiple tasks with single thread, displaying two threads acting upon a single object, checking currentthread() in multithreading, creating a client that receive messages from the server and multithreaded implementation on divide conquer algorithm.
Java Program Showing Execution of Multiple Tasks with a Single Thread
Java Program Showing Two Threads Working Simultaneously Upon Two Objects
Java Program Showing Two Threads Acting Upon a Single Object
Java Program with 2 Threads Which Prints Alternatively
Java Program to Start One Thread More than Once
Java Program to Check CurrentThread in Multi Threading Concept
Java Program to Create a Server with 2 Threads to Communicate with Serveral Clients
Java Program to Create a Client That Receive Message From the Server
Java Program of a Multithreaded Implementation of Any Parallelized Divide-Conquer Algorithm
9. Java Examples on Thread Group
Java thread is a member of a thread group. Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually. In general, Thread group is a data structure that controls the state of a collection of threads as a whole. The section contains java programs on changing the priority of a thread group, finding the parent of a thread group, getting the count of actively running threads in thread group and demonstrating the creation of thread groups.
Java Program to Know the Parent of a Thread Group or Thread Group
Java Program to Get the Number of Threads Actively Running in a Thread Group
Java Program to Change the Maximum Priority of a Thread Group
Java Program to Demonstrate the Creation of Thread Groups and Some Methods Which Act on Thread Groups
10. Java Examples on Running Threads
The run() is used to perform action for a thread. getId() returns the id of the thread. This section contains Java programs on displaying the running thread using run() and currentthread() method, getting the id of running thread by using getId(), displaying thread status, interrupting running thread by using interrupt() method and monitors the thread status.
Java Program to Get the Id of the Running Thread
Java Program to Display all Running Thread
Java Program to Display Thread Status
Java Program to Interrupt a Running Thread
Java Program to Monitor a Thread’s Status
11. Java Examples on Object Class using Threads
Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection. wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. The Java programs in this section to verify given thread is a daemon or nor, specifying the name after object creation, displaying Wait(), Notify() and NotifyAll() in object class.
Java Program to Specify the Name After Creating the Object
Java Program to Verify Thread is Daemon or Not
Java Program to Show that Can we Aquire Lock on Class/ Class Object or Not
Java Program to Show Why Wait(), Notify() and NotifyAll() is Defined in Object Class
Java Program to Get the Thread Group Object You Need to Invoke the Method with Thread Object
12. Java Examples on Producer and Consumer Problem
This section contains java programs on producer and consumer problem. It explains the use of producer and consumer problem, solving the problem using threads, checks whether consumer thread is informing or not when data production is over.
Java Program to Use Producer and Consumer Problem
Java Program to Solve the Producer Consumer Problem Using Thread
Java Program in Which Consumer Thread is Informed Immediately When the Data Production is Over
Java Program to Illustratate Where the Consumer Thread Check Whether the Data Production is Over
Java Program to Show That the Consumer Thread is Informed Immediately When the Data Production is Over