Professional Documents
Culture Documents
Concurrency 1 Chapter
Concurrency 1 Chapter
Concurrency 1 Chapter
SOLID is a structured design approach that ensures your software is modular and easy
to maintain, understand, debug, and refactor.
Following SOLID also helps save time and effort in both development and
maintenance.
SOLID prevents your code from becoming rigid and fragile, which helps you build
long-lasting software.
1.1 Single responsibility principle
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
Commonly used methods of Thread class
public void run(): is used to perform action for a thread
public void join(): waits for a thread to die
public int getId(): returns the id of the thread
public Thread.State getState(): returns the state of the thread
Runnable interface
The Runnable interface should be implemented by any class whose instances are intended to be
executed by a thread.
Runnable interface have only one method named run().
public void run(): is used to perform action for a thread
• class Multi3 implements Runnable{
• public void run(){
• System.out.println("thread is running...");
• }
•
• public static void main(String args[]){
• Multi3 m1=new Multi3();
• Thread t1 =new Thread(m1);
• t1.start();
• }
• }
1.3 Creating Thread
• Thread a line of execution within a program.
• Each program can have multiple associated threads.
• Each thread has a priority which is used by thread scheduler to determine
which thread must run first.
• Java provides a thread class that has various method calls inorder to manage the
behavior of threads.
• Public class Thread
extends object
implements Runnable
Thread class provide constructors and methods to create and perform operations on
a thread.
Thread class extends Object class and implements Runnable interface
Starting a thread:
If the number of papers in the printer is less than the number of entered
input, then we will call wait() method for the thread, meanwhile,
another thread will add more pages in the printer and notify the current
thread by notify() method.
1.6 Thread Pool & Executor Service