Threads
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.
Computer Playing Music Other Video Player Tasks My Super Browse Computer Internet NotePad Downloading Playing Files Game
Music Player Playing Managing Songs Playlists Music Player Downloading Accessing Updates Internet
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.
Thread – Life Cycle New Program starts thread Unlock signal Thread completes task Waiting Runnable Terminated Await lock Await sleep Interval express Timed Waiting
Thread – Methods New Blocked t.sleep(), t.start() t.wait() Run method ends t.run() Ready Running Terminated
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.
Creation of threads in multiple ways Threads can be created in two ways. • Implementing Runnable interface. • Extending Thread class
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(); } }
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(); } }
Thread Class methods • int getPriority() • void start() • void setPriority(int newpriority) • void run() • void sleep(long millis) • void join(long millis) • void wait() • void notify() • Thread currentThread() • void notifyAll() • Thread getState() • void setName(String name) • void stop() • String getState() • void yield()
Multithreading • When two or more threads are running in a process simultaneously then it is called Multithreading.
Multithreading Example package com.edureka.multithreading; public class Second extends Thread{ public void run() package com.edureka.multithreading; { public class Second extends Thread{ System.out.println("This is public void run() first thread"); { } System.out.println("This is } second thread"); } }
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(); } }
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’.
How to synchronize the object? synchronized(object) { Synchronized //Statements } block synchronized returntype methodname() Synchronized { method //Statements }
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()
Deadlock Object • Deadlock is a situation in 1 waiting which a thread has got an object and waiting for an object locked by another Thread2 thread which is waiting for Thread1 the object locked by first thread. waiting Object 2
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.
•Q& A..?
Thanks..!

Java class 6

  • 1.
  • 2.
    Thread • A threadis a part of execution in a program. • The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
  • 3.
    Computer Playing Music Other Video Player Tasks My Super Browse Computer Internet NotePad Downloading Playing Files Game
  • 4.
    Music Player Playing Managing Songs Playlists Music Player Downloading Accessing Updates Internet
  • 5.
    Why do weuse threads? • Thread is a light weight process as it shares the same memory and the resources of the process in which it is created.
  • 6.
    Thread – LifeCycle New Program starts thread Unlock signal Thread completes task Waiting Runnable Terminated Await lock Await sleep Interval express Timed Waiting
  • 7.
    Thread – Methods New Blocked t.sleep(), t.start() t.wait() Run method ends t.run() Ready Running Terminated
  • 8.
    Thread Priorities • Whenmultiple 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.
  • 9.
    Creation of threadsin multiple ways Threads can be created in two ways. • Implementing Runnable interface. • Extending Thread class
  • 10.
    Implementing Runnable Interface packagecom.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(); } }
  • 11.
    Extending Thread Class packagecom.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(); } }
  • 12.
    Thread Class methods •int getPriority() • void start() • void setPriority(int newpriority) • void run() • void sleep(long millis) • void join(long millis) • void wait() • void notify() • Thread currentThread() • void notifyAll() • Thread getState() • void setName(String name) • void stop() • String getState() • void yield()
  • 13.
    Multithreading • When twoor more threads are running in a process simultaneously then it is called Multithreading.
  • 14.
    Multithreading Example package com.edureka.multithreading; publicclass Second extends Thread{ public void run() package com.edureka.multithreading; { public class Second extends Thread{ System.out.println("This is public void run() first thread"); { } System.out.println("This is } second thread"); } }
  • 15.
    Multithreading Example package com.edureka.multithreading; publicclass 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(); } }
  • 16.
    Synchronization • The processof making only one thread access the object when two or more threads are accessing the same object is called Synchronization. • Keyword - ‘synchronized’.
  • 17.
    How to synchronizethe object? synchronized(object) { Synchronized //Statements } block synchronized returntype methodname() Synchronized { method //Statements }
  • 18.
    Inter-Thread Communication • Theprocess 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()
  • 19.
    Deadlock Object • Deadlock is a situation in 1 waiting which a thread has got an object and waiting for an object locked by another Thread2 thread which is waiting for Thread1 the object locked by first thread. waiting Object 2
  • 20.
    Deadlock Example • Player1has 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.
  • 21.
  • 22.

Editor's Notes

  • #8 Ready is not a state