CH 6
CH 6
Multithreading
• Thread Class:
• The Thread class provides constructors and methods for creating and operating on threads. The thread extends the Object and implements
the Runnable interface.
• Any class with instances that are intended to be executed by a thread should implement the Runnable interface. The Runnable interface has
only one method, which is called run().
• The run() method is not called by the thread you created. Instead, it is
called by the thread that created the class.
• Commonly used Constructors of Thread class:
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)
1.Only one thread can read and write a shared variable at a time. When one
thread is accessing a shared variable, other threads should wait until the first
thread is done. This guarantees that the access to a shared variable is Atomic,
and multiple threads do not interfere.
Volatile and synchronized keywords are used for interference and memory
consistency error control
java.lang.Runnable is an interface that is to be implemented by a class whose
instances are intended to be executed by a thread. There are two ways to start a
new Thread – Subclass Thread and implement Runnable. There is no need of
subclassing a Thread when a task can be done by overriding only run() method of
Runnable.
public class RunnableDemo {
// 3
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
// 9
System.out.println("Priority of the thread th3 is : " + th3.getPriority());
// Main thread
}
}
N Constructor Description
o.
1) ThreadGroup(String name) creates a thread group with given name.
2) ThreadGroup(ThreadGroup parent, creates a thread group with a given parent group and name.
String name)
Daemon Threads
A Daemon thread is a background service thread which runs as a low priority
thread and performs background operations like garbage collection. JVM
exits if only daemon threads are remaining.
A deprecated feature
means it is not in active
development and may be
removed in a future update.
Synchronizing threads
public class Table public class Thread1 extends Thread
{ {
void printTable(int n) // Here, method is not synchronized. Table t; // Declaration of variable t of class type Table.
{
for(int i = 1; i <= 5; i++) // Declare one parameterized constructor and pass variable t as
{ a parameter.
System.out.println(n * i); Thread1(Table t)
try {
{ this.t = t;
Thread.sleep(400); }
} public void run()
catch(InterruptedException ie) {
{ t.printTable(2);
System.out.println(ie); }
} }
}}
}
public class Thread2 extends Thread public class SynchronizedMethod
{ {
Table t; public static void main(String[] args)
Thread2(Table t) {
{ // Create an object of class Table.
this.t = t; Table obj = new Table();
} Thread1 t1 = new Thread1(obj);
public void run() Thread2 t2 = new Thread2(obj);
{ t1.start();
t.printTable(10); t2.start();
} }
} }
public class Table public class Thread1 extends Thread
{ {
synchronized void printTable(int n) // Here, method is Table t; // Declaration of variable t of class type Table.
synchronized.
{ // Declare one parameterized constructor and pass variable t as
for(int i = 1; i <= 5; i++) a parameter.
{ Thread1(Table t)
System.out.println(n * i); {
try this.t = t;
{ }
Thread.sleep(400); public void run()
} {
catch(InterruptedException ie) t.printTable(2);
{ }
System.out.println(ie); }
}
}}
}
public class Thread2 extends Thread public class SynchronizedMethod
{ {
Table t; public static void main(String[] args)
Thread2(Table t) {
{ // Create an object of class Table.
this.t = t; Table obj = new Table();
} Thread1 t1 = new Thread1(obj);
public void run() Thread2 t2 = new Thread2(obj);
{ t1.start();
t.printTable(10); t2.start();
} }
} }
Synchronized Block
}
class ThreadB extends Thread {
int totalBalance = 0;