22
Threads

Java class 6

  • Upload
    edureka

  • View
    486

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Java class 6

Threads

Page 2: Java class 6

Thread

• A thread is a part of execution in a program. 

• The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Page 3: Java class 6

Computer

Page 4: Java class 6

Music Player

Page 5: Java class 6

Why do we use threads?

• Thread is a light weight process as it shares the same memory and the resources of the process in which it is created.

Page 6: Java class 6

Runnable

Terminated

NewTimed Waiting

WaitingProgram starts thread

Unlock signal Thread completes task

Await lock

Await sleep Interval express

 

Thread – Life Cycle

Page 7: Java class 6

Thread – Methods

New

Ready

Blocked

Running

Terminated

t.notify()

notifyAll()

t.start()

t.run()Run method ends

t.sleep(),t.wait()

Page 8: Java class 6

Thread Priorities

• When multiple threads are running the order of execution of threads depend on priority given to thread.

• Priority values ranges from 1 to 10.(default-5).• setPriority(int priority) – method to set priority.

Page 9: Java class 6

Creation of threads in multiple ways

Threads can be created in two ways.• Implementing Runnable interface.• Extending Thread class

Page 10: Java class 6

Implementing Runnable Interface

package com.edureka.threadsrunnable;public class ThreadClass implements Runnable {public void run() {        System.out.println("Hello from a thread!");    }} package com.edureka.threadsrunnable;

public class Main{public static void main(String[] args) {    ThreadClass obj = new ThreadClass(); //object of class ThreadClass    Thread thread_runnable = new Thread(obj);    thread_runnable.start();    }}

Page 11: Java class 6

Extending Thread Class

package com.edureka.threads;public class ThreadClass extends Thread {public void run() {        System.out.println("Hello from a thread!");    }}

package com.edureka.threads;public class Main{public static void main(String args[]) {    ThreadClass obj = new ThreadClass(); //object of class ThreadT    Thread thread_extend = new Thread(obj);    thread_extend.start();    }}

Page 12: Java class 6

Thread Class methods

• int getPriority()• void setPriority(int newpriority)

• void sleep(long millis)• void join(long millis)

• Thread currentThread()• Thread getState()

• void setName(String name)• String getState()

• void start()• void run()

• void wait()• void notify()• void notifyAll()

• void stop()• void yield()

Page 13: Java class 6

Multithreading

• When two or more threads are running in a process simultaneously then it is called Multithreading.

Page 14: Java class 6

Multithreading Example

package com.edureka.multithreading;public class Second extends Thread{public void run(){

System.out.println("This is first thread");}

}

package com.edureka.multithreading;public class Second extends Thread{public void run(){

System.out.println("This is second thread");}

}

Page 15: Java class 6

Multithreading Example

package com.edureka.multithreading;public class Main extends Thread{public static void main(String[] args){

Thread thread1 = new Thread(new One());Thread thread2 = new Thread(new Second());

thread1.start();thread2.start();

}}

Page 16: Java class 6

Synchronization

• The process of making only one thread access the object when two or more threads are accessing the same object is called Synchronization.

• Keyword - ‘synchronized’.

Page 17: Java class 6

How to synchronize the object?

   Synchronized method

synchronized(object){

//Statements}

synchronized returntype methodname(){

//Statements}

Synchronized block

Page 18: Java class 6

Inter-Thread Communication

• The process of communication between multiple threads is called Inter-thread communication.

• In this process of communication when one thread is executing the other thread will wait for the thread to be executed.

• The methods used for communication are:wait()notify()notifyAll()

Page 19: Java class 6

Deadlock

• Deadlock is a situation in which a thread has got an object and waiting for an object locked by another thread which is waiting for the object locked by first thread. 

Thread1Thread2

Object1

Object2

waiting

waiting

Page 20: Java class 6

Deadlock Example

• Player1 has got a bat to play and waiting for a ball.

• Player2 has got a ball to play and waiting for a bat.

• Both are waiting for the resource which are held by the other player. This leads to deadlock. 

Page 21: Java class 6

•Q& A..?

Page 22: Java class 6

Thanks..!