0% found this document useful (0 votes)
4 views11 pages

Multithreading & Synchronization

The document provides an overview of multithreading and synchronization in Java, explaining key concepts such as threads, their life cycle, and methods to create them. It details two approaches for creating threads: extending the Thread class and implementing the Runnable interface, along with examples for each. Additionally, it covers synchronization mechanisms to manage access to shared resources, including synchronized methods and blocks, with an example involving a banking system.

Uploaded by

bbmaharana11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Multithreading & Synchronization

The document provides an overview of multithreading and synchronization in Java, explaining key concepts such as threads, their life cycle, and methods to create them. It details two approaches for creating threads: extending the Thread class and implementing the Runnable interface, along with examples for each. Additionally, it covers synchronization mechanisms to manage access to shared resources, including synchronized methods and blocks, with an example involving a banking system.

Uploaded by

bbmaharana11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java By Venkatesh Mansani Naresh i Technologies

Multithreading & Synchronization


Multithreading:
Execution of more than one thread at a time is called as multithreading.

Thread:
A thread is a piece of code that executes independently.
Every program contains at least one thread. i.e. main thread
Main thread default name is main only.
Main thread default priority is normal priority(Priority number is 5)

There are two ways to create a new thread:


1) By extending java.lang.Thread class
2) By implementing a java.lang.Runnable interface

Life cycle of a thread (or) thread states:


Whenever thread class constructor is called new thread will born.
Thread comes to a ready state whenever start() method is called.
Thread will run whenever run() method is called.
Thread comes to a sleeping state whenever sleep() method is called.
Sleeping thread will wake up automatically whenever time interval is finished.
Thread is suspended whenever suspend() method is called.
Suspended thread will run whenever resume() method is called.
Thread comes to a waiting state whenever wait() method is called.
Waiting thread will run whenever notify() method is called.
Thread will die whenever destroy() method is called.

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

Program to get current thread information:


class Demo
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
System.out.println(t.getName());
System.out.println(t.getPriority());
t.setName(“demo”);
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t.getName());
System.out.println(t.getPriority());
}
}

Program to demonstrate sleep() method:


class Demo
{
Public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
try{
Thread.sleep(1000);

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

}catch(Exception e)
{
System.err.println(e);
}
}
}
}

Steps to develop multithreaded application by extending


java.lang.Thread class:
1) Create a class that extends java.lang.Thread class
2) Override run() method.
Note: run() method given as a null body method to write child thread task code.
3) Write child thread task code in a run() method.
4) Write main() method
5) Create an object of current class.
6) Call start() method.
Note: start() method implicitly calls run() method.
7) Write main thread task code in a main() method.

Example:
class Demo extends Thread
{
public void run()
{
try{

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

for(int i=1;i<=10;i++)
{
System.out.println("Child Thread: "+i);
Thread.sleep(1000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
public static void main(String args[])
{
try{
Demo d=new Demo();
d.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread: "+i);
Thread.sleep(1000);
if(i==5)
d.suspend();
if(i==10)
d.resume();
}

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

}catch(Exception e)
{
System.err.println(e);
}
}
}

Steps to create multithread application by implementing


java.lang.Runnable interface:
1) Create a class that implements java.lang.Runnable interface.
2) Override run() method.
3) Write child thread task code in a run() method.
4) Write main() method
5) Create an object of current class & assign to Runnable reference
6) Create an object Thread class by passing Runnable reference
7) Call start() method.
8) Write main thread task code in a main() method.

Example:
class Test implements Runnable
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

System.out.println("JavaEE");
Thread.sleep(1000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
}
class Demo implements Runnable
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
System.out.println("Core Java");
Thread.sleep(2000);
}
}catch(Exception e)
{
System.err.println(e);
}
}

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

public static void main(String args[])


{
try{
Runnable r=new Demo();
Thread t=new Thread(r);
t.start();
Runnable r2=new Test();
Thread t2=new Thread(r2);
t2.start();
for(int i=1;i<=10;i++)
{
System.out.println("Advanced Java");
Thread.sleep(3000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
}

Synchronization:
It is a mechanism that allows to access a shared resource only one thread at a
time.
There are two ways to synchronize the code:
1) synchronizing a method

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

2) synchronizing block of code

1) synchronizing a method:
Syntax:
synchronized ReturnType MethodName(arg1, arg2, ......)
{
================
================
================
}

2) synchronizing a block of code:


Syntax:
ReturnType MethodName(arg1, arg2, ......)
{
================
================
synchronized(Object)
{
================
================
}
================
================
}

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

Example:
class Bank
{
float balance=5000.00f;
synchronized void withdraw(float amount)
{
try{
System.out.println("Withdraw Started");
if(balance<amount)
{
System.out.println("Less Balance, Waiting for Deposit");
wait();
}
balance=balance-amount;
System.out.println("Withdraw Completed");
}catch(Exception e)
{
System.err.println(e);
}
}
synchronized void deposit(float amount)
{
System.out.println("Deposit Started");
balance=balance+amount;

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

System.out.println("Deposit Completed");
notify();
}
}
class Customer1 extends Thread
{
Bank b;
Customer1(Bank b)
{
this.b=b;
}
public void run()
{
b.withdraw(8000.00f);
}
}
class Customer2 extends Thread
{
Bank b;
Customer2(Bank b)
{
this.b=b;
}
public void run()

Java By Venkatesh Mansani [email protected]


Java By Venkatesh Mansani Naresh i Technologies

{
b.deposit(5000.00f);
}
}
class Demo
{
public static void main(String args[])
{
Bank b=new Bank();
Customer1 c1=new Customer1(b);
c1.start();
Customer2 c2=new Customer2(b);
c2.start();
}
}

Java By Venkatesh Mansani [email protected]

You might also like