SRI RAMAKRISHNA ENGINEERING COLLEGE
[Educational Service : SNR Sons Charitable Trust]
[Autonomous Institution, Reaccredited by NAAC with ‘A+’ Grade]
[Approved by AICTE and Permanently Affiliated to Anna University, Chennai]
[ISO 9001:2015 Certified and all Eligible Programmes Accredited by NBA]
VATTAMALAIPALAYAM, N.G.G.O. COLONY POST, COIMBATORE – 641 022.
Department of Information Technology
20IT252 – Java Programming (TcL)
PRESENTATION BY
[Link], AP/IT
20IT252 – JAVA PROGRAMMING
Module I : INTRODUCTION TO JAVA CONCEPTS 10
Java Evolution -Data Types - Variables - Control Statements - Methods
and Classes - Arrays - String Handling - Inheritance - Exception
Handling - Multithreaded Programming - Interfaces - Packages.
* JAVA PROGRAMMING 2
Multitasking
▪ Multitasking is a process of executing multiple tasks simultaneously.
▪ use multitasking to utilize the CPU.
▪ can be achieved in two ways:
• Process-based Multitasking (Multiprocessing)
• Thread-based Multitasking (Multithreading)
* JAVA PROGRAMMING 3
* JAVA PROGRAMMING 4
Types of Multitasking
1) Process-based Multitasking (Multiprocessing)
•Each process has an address in memory.
•In other words, each process allocates a separate memory area.
•A process is heavyweight.
•Cost of communication between the process is high.
•Switching from one process to another requires some time for saving and loading register,
memory maps, updating lists, etc.
2) Thread-based Multitasking (Multithreading)
•Threads share the same address space.
•A thread is lightweight.
•Cost of communication between the thread is low.
* JAVA PROGRAMMING 5
Multithreaded Programming
* JAVA PROGRAMMING 6
* JAVA PROGRAMMING 7
* JAVA PROGRAMMING 8
What is Multithreading in Java?
Multithreading in Java is a process of executing two or more threads simultaneously to
maximum utilization of CPU.
Multithreaded applications execute two or more threads run concurrently.
Hence, it is also known as Concurrency in Java.
Each thread runs parallel to each other.
Mulitple threads don’t allocate separate memory area, hence they save memory.
Also, context switching between threads takes less time.
* JAVA PROGRAMMING 9
Thread Life Cycle in Java
Stages of life cycle of thread :
1) New
2) Runnable
3) Running
4) Waiting
5) Dead
* JAVA PROGRAMMING 10
Stages of Thread Life Cycle
New: In this phase, the thread is created using class “Thread class”. It remains in this
state till the program starts the thread. It is also known as born thread.
Runnable: In this page, the instance of the thread is invoked with a start method. The
thread control is given to scheduler to finish the execution. It depends on the scheduler,
whether to run the thread.
Running: When the thread starts executing, then the state is changed to “running” state.
The scheduler selects one thread from the thread pool, and it starts executing in the
application.
Waiting: This is the state when a thread has to wait. As there multiple threads are
running in the application, there is a need for synchronization between threads. Hence,
one thread has to wait, till the other thread gets executed. Therefore, this state is referred
as waiting state.
Dead: This is the state when the thread is terminated. The thread is in running state and
as soon as it completed processing it is in “dead state”.
* JAVA PROGRAMMING 11
Thread Creation
Threads can be created by using two mechanisms :
❖Extending the Thread class
❖Implementing the Runnable Interface
* JAVA PROGRAMMING 12
1) Thread creation by extending the Thread class
❖Create a class that extends the [Link] class.
❖ This class overrides the run() method available in the Thread class.
❖A thread begins its life inside run() method.
❖Create an object of our new class and call start() method to start the
execution of a thread.
❖Start() invokes the run() method on the Thread object.
* JAVA PROGRAMMING 13
Example
class Hi extends Thread class Hello extends Thread
{ {
public void run() public void run()
{ {
for(int i=1;i<=5;i++){ for(int i=1;i<=5;i++){
[Link]("Hi"); [Link]("Hello");
try{ [Link](500);} try{ [Link](500);}
catch(Exception e){ } catch(Exception e){ }
} }
} }
} }
* JAVA PROGRAMMING 14
..contd
public class Main { Output
public static void main(String[ ] args) { Hi
Hello
// [Link]("Thread implementation");
Hi
Hi obj1=new Hi(); Hello
Hi
Hello obj2=new Hello(); Hello
[Link](); Hi
Hello
[Link](); Hi
Hello
}
}
* JAVA PROGRAMMING 15
2)Thread creation by implementing the Runnable Interface
❖Create a new class which implements [Link] interface
❖Override run() method.
❖Then instantiate a Thread object and call start() method on this object.
Note:
1) Runnable interface has only run() method not the start() method.
2) To invoke the run() method, we need to create Thread object, using that call the start()
method.
3) While creating Thread object, pass the runnable interface reference as argument.
* JAVA PROGRAMMING 16
Example
class Hi implements Runnable class Hello implements Runnable
{ {
public void run() public void run()
{ {
for(int i=1;i<=5;i++){ for(int i=1;i<=5;i++){
[Link]("Hi"); [Link]("Hello");
try{ [Link](500);} try{ [Link](500);}
catch(Exception e){ } catch(Exception e){ }
} }
} }
} }
* JAVA PROGRAMMING 17
..contd
public class Main { Output
public static void main(String[ ] args) { Hi
Hello
// [Link]("Thread implementation");
Hi
Hi obj1=new Hi(); Hello
Hi
Hello obj2=new Hello(); Hello
Thread t1=new Thread(obj1); Hi
Hello
Thread t2=new Thread(obj2); Hi
Hello
[Link]();
[Link]();
}
}
* JAVA PROGRAMMING 18
Thread Class vs Runnable Interface
If we extend the Thread class, our class cannot extend any other class because Java
doesn’t support multiple inheritance. But, if we implement the Runnable interface, our
class can still extend other base classes.
We can achieve basic functionality of a thread by extending Thread class because it
provides some inbuilt methods like yield(), interrupt() etc. that are not available in
Runnable interface.
Using runnable will give you an object that can be shared amongst multiple threads.
* JAVA PROGRAMMING 19
* JAVA PROGRAMMING 20