Module Vi Oops 1
Module Vi Oops 1
class main{
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
}
Current thread: Thread[#1,main,5,main]
}
• The first way to create a thread is to create a new class that extends Thread class
• The extending class must override the run( ) method, which is the entry point for the
new thread.
• Create an instance of that class.
• It must also call start( ) to begin execution of the new thread.
class Fact extends Thread { class Sum extends Thread { class Factsum{
int n; int n; public static void main(String
Fact(int n) Sum(int n) args[]) {
{ { Fact t1=new Fact(5);
this.n=n; this.n=n; Sum t2=new Sum(4);
} }
public void run() { public void run() { t1.start();
int fact=1; int sum=0; t2.start();
for(int i = 1; i <=n; i++) { for(int i = 1; i <=n; i++) {
fact=fact*i; sum=sum+i; }
} } }
System.out.println("Factorial System.out.println("Sum is:"+sum);
is:"+fact); }
} }
}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Creating a Thread using Implementing the Runnable interface.
• The easiest way to create a thread is to create a class that implements the Runnable
interface and override run() method.
class RunnableDemo {
class NewThread implements Runnable{ public static void main(String args[]) {
public void run() { NewThread ob1=new NewThread();
for(int i = 0; i < 3; i++) { NewThread ob2=new NewThread();
System.out.println(Thread.currentThread(). NewThread ob3=new NewThread();
getName()+ ": " + i); Thread t1=new Thread(ob1); Thread-2: 0
} Thread t2=new Thread(ob2); Thread-2: 1
} Thread t3=new Thread(ob3); Thread-0: 0
Thread-0: 1
} t1.start(); Thread-1: 0
t2.start(); Thread-2: 2
t3.start(); Thread-0: 2
Thread-1: 1
}} Thread-1: 2
Create a Thread “BankAccount” using Thread class. Bob and Joy are having joint
account with an initial balance of 100000 and they both together allowed to withdraw.
Create one thread for each of them and concurrently perform withdrawal. Create a class
bankactivity with two methods “withdrawal and “balance”. Create a synchronized
withdrawal method so that While withdrawing no other process can be done.
The method bookticket() has this form: bookticket(String name, int numberoftickets).
In this method, check if tickets are available; if available then the particular person will
book that number of tickets otherwise print the message that there are no tickets for
booking. Update the number of tickets after booking. Consider initially
numberoftickets=8. Also, write down the output of the program with synchronization
s1.start();
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE 20
Inter Thread Communication
• In Java, interthread communication is a mechanism that allows threads to communicate
with each other by signaling or notifying each other about certain events or conditions.
• It is typically used when one thread needs to wait for another thread to complete a
certain task.
• Java provides a built-in mechanism for interthread communication through the wait(),
notify(), and notifyAll() methods.
• These methods can only be called from within a synchronized context.
• wait(): When a thread calls the wait() method on an object, it enters a waiting state
until another thread calls notify() or notifyAll() on the same object.
• notify(): The notify() method is used to wake up a single thread that is waiting on the
same object.
• notifyAll(): The notifyAll() method wakes up all threads that are waiting on the same
object.