You are on page 1of 12

Multithreaded Programming

Payel Saha
Department Of Computer Science &
Applications
North Bengal St.Xavier’s College
Date: 21.4.2020
Thread in java:
• A thread is a lightweight subprocess, the smallest unit
of processing.
• It is a separate path of execution.
• Threads are independent.
• If there occurs exception in one thread, it does not
affect other threads.
• It uses a shared memory area.
• All main programs in earlier examples can be called
single threaded programs.
• Every program will have at least one thread.
In the above figure, a thread is executed inside the process. There is context switching
between threads. There can be multiple processes inside the Os.
Advantage of Java multithreading
• It does not block the user because threads are
independent and we can perform multiple
operations at the same time.
• We can perform many operations together, so it
saves time.
• Threads are independent, so it doesn't affect
other threads if an exception occurs in a single
thread.
How to create a thread
The run() method should be invoked by an
object of the concerned thread. This can be
achieved by creating the thread and initiating
it with the help of another thread method
start().
There are two ways to create a thread:
• By extending Thread class
• By implementing Runnable interface.
Creating threads using thread class
class Single extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println(i);
}
}
}
class St
{
public static void main(String s[])
{
Single t=new Single();
t.start();
}
}
Creating threads using runnable
interface
class Single implements Runnable
{
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println(i);
}
}
}
class Stu
{
public static void main(String args[])
{
Single s=new Single();
Thread t=new Thread(s);
t.start();
}
}
Some methods of thread
• public static void yield()
Causes the currently running thread to yield
to any other threads of the same priority that
are waiting to be scheduled.
public static void sleep(long millisec)
• Causes the currently running thread to block
for at least the specified number of
milliseconds.
public final void join(long millisec)
• The current thread invokes this method on a
second thread, causing the current thread to
block until the second thread terminates or
the specified number of milliseconds passes.
public final boolean isAlive()
• Returns true if the thread is alive, which is any
time after the thread has been started but
before it runs to completion.
Thread Priorities
• Every Java thread has a priority that helps the
operating system determine the order in which threads
are scheduled.
• Java thread priorities are in the range between
MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a
constant of 10). By default, every thread is given
priority NORM_PRIORITY (a constant of 5).
• Threads with higher priority are more important to a
program and should be allocated processor time before
lower-priority threads. However, thread priorities
cannot guarantee the order in which threads execute
and are very much platform dependent.
. Write a program in java to demonstrate priorities among multiple threads.
import java.lang.*;
class ThreadDemo extends Thread
{
public void run()
{
System.out.println("Inside run method");
}
public static void main(String s[])
{
ThreadDemo t1=new ThreadDemo();
ThreadDemo t2=new ThreadDemo();
ThreadDemo t3=new ThreadDemo();
System.out.println("t1 thread priority:"+t1.getPriority());
System.out.println("t2 thread priority:"+t2.getPriority());
System.out.println("t3 thread priority:"+t3.getPriority());
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
System.out.println("t1 thread priority:"+t1.getPriority());
System.out.println("t2 thread priority:"+t2.getPriority());
System.out.println("t3 thread priority:"+t3.getPriority());
System.out.println(Thread.currentThread().getName());
System.out.println("Main thread priority:"+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority:"+Thread.currentThread().getPriority());
}
}
Output

You might also like