Java Multithreading PDF
Java Multithreading PDF
T OP S T ORY S UBMIT
You have 2 free stories left this month. Sign up and get an extra one for free.
Java Multithreading
Kasun Dissanayake Follow
May 28, 2019 · 8 min read
Examples:
Games are very good examples of threading. You can use multiple
objects in games like cars, motorbikes, animals, people, etc. All these
objects are nothing but just threads that run your game application.
What is Thread?
A thread is similar to a program that has a single flow of control. Every
program has at least one thread and the thread has a beginning, body
and an end. And it executes commands sequentially. So Multithreading
means executing more than one commands in parallel. The special thing
is Java supports for the Multithreading.
Main Thread
Thread A
Thread B
Thread C
Now first of all the main Thread initiate. Then Thread A, B, and C run
concurrently and share the resource jointly. Since threads in Java are
subprograms of the main application program and share the same
memory space, they are known as Light- Weight Threads.
Runnable State — Thread is ready for execution and waiting for the
availability of processor.
Running State — The processor has given it’s time to the execution.
Dead State — Running Thread ended his life cycle and completed
executing the run method.
Thread Exceptions
Sleep() method should enclose in a try block and follow by a catch block.
This is important because the sleep method throws an exception which
should be caught. If we do not catch the exception the program will not
compile.
The approach can be used depending on what the class we are creating
require.
Steps:
Declare the class as extending the Thread Class.
package threadtutorial;
/**
*
* @author Kasun Dissanayake
*/
Here Multi t1 = new Multi(); statement creates the Object. The Thread
that will run this Object is not yet running. The Thread is in a newborn
state.
Now t1.start(); statement leads the thread move to the runnable state.
Then the Java interpreter will schedule the thread to run by invoking this
run() method. Now the Thread is in running state.
Run this code segment then you will get a result like this.
The thread will move to the dead state automatically when it reaches the
end of its method.
Example :
package threadtutorial;
/**
*
* @author Kasun Dissanayake
*/
}
}
When we use the sleep() method, a thread gets started after a specified
time interval, unless it is interrupted.
For wait(), the waking up process is a bit more complicated. We can wake
the thread by calling either the notify() or notifyAll() methods on the
monitor that is being waited on.
Use notifyAll() instead of notify() when you want to wake all threads that
are in the waiting state. Similarly to the wait()method itself, notify(), and
notifyAll() have to be called from the synchronized context.
Example :
package threadtutorial;
/**
*
* @author Kasun Dissanayake
*/
}
}
Thread Priority
In Java, we can assign a priority to a Thread which affects the order in
the schedule for running. The Threads which have the same priority has
an equal priority (This done by the Java scheduler) and they share the
processor on a first come first serve manner.
Threadname.setpriority(int number);
MIN_PRIORITY = 1
MAX_PRIORITY = 10
Exercise:
A program which includes Class A and Class B. The Class A uses a thread
to display numbers from 1–10 and Class B uses a Thread to display
numbers 10 -1. Priority as Class A Thread — Max and Class B Thread
— Min
package threadtutorial;
/**
*
* @author Kasun Dissanayake
*/
a.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.MIN_PRIORITY);
a.start();
b.start();
Execute the code. Then you will get a result like this.
Synchronization
In the run, method Threads try to use some resources which lie inside
the run method as well as outside the run method. If two Threads try to
reach the same resource there may be happening a problem. Then Java
gives us a technique to overcome this problem named
Synchronization. Here we can use a synchronized method to
implement Synchronization mechanism.
Example:
}
}
Execute the code and you will get a result like this.
Steps:
Declare the class as implementing the Runnable Interface.
package threadtutorial;
/**
*
* @author Kasun Dissanayake
*/
}
}
As always, you can check out the examples provided in this article over
on GitHub.
Thank you!
981 claps
WRIT T EN BY
Reference copy , Shallow Encapsulation in Python Django and Stripe 3 Ways to Execute
copy and Deep Copy Coder’s Cat in Better Integration Commands in a Docker
Nitish Prasad in T he Startup Programming Ordinary Coders in T he Container
Startup Nasi Jofche in Better
Programming
A Deep Dive into the How to build a GraphQL Multiple Tool Versions Picking the Right
Flutter Animations server with Swift and via Homebrew Arduino
package Vapor Andreas Siegel James Lewis in Hackster Blog
Martin Rybak in Flutter NYC Alexander Steiner