Threads
Thread• A thread is a part of execution in a program.• The Java Virtual Machine allows an application to have  multiple th...
Computer                 Playing              Music  Other           Video               Player  Tasks                    ...
Music Player    Playing               Managing     Songs                Playlists                 Music                 Pl...
Why do we use threads? • Thread is a light weight   process as it shares the   same memory and the   resources of the   pr...
Thread – Life Cycle                                           New                                                 Program ...
Thread – Methods            New             Blocked                                 t.sleep(),t.start()                   ...
Thread Priorities• When multiple threads are running the order of  execution of threads depend on priority given to  threa...
Creation of threads in multiple waysThreads can be created in two ways.• Implementing Runnable interface.• Extending Threa...
Implementing Runnable Interfacepackage com.edureka.threadsrunnable;public class ThreadClass implements Runnable {public vo...
Extending Thread Classpackage com.edureka.threads;public class ThreadClass extends Thread {public void run() {    System.o...
Thread Class methods• int getPriority()                   • void start()• void setPriority(int newpriority)               ...
Multithreading• When two or more threads are running in a  process simultaneously then it is called  Multithreading.
Multithreading Examplepackage com.edureka.multithreading;public class Second extends Thread{public void run()             ...
Multithreading Examplepackage com.edureka.multithreading;public class Main extends Thread{public static void main(String[]...
Synchronization• The process of making only one thread access  the object when two or more threads are  accessing the same...
How to synchronize the object?   synchronized(object)   {                                        Synchronized           //...
Inter-Thread Communication• The process of communication between multiple threads is  called Inter-thread communication.• ...
Deadlock                                             Object• Deadlock is a situation in                   1               ...
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...
•Q& A..?
Thanks..!
Upcoming SlideShare
Loading in …5
×

Java class 6

967 views
752 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
967
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
124
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Ready is not a state
  • Java class 6

    1. 1. Threads
    2. 2. 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.
    3. 3. Computer Playing Music Other Video Player Tasks My Super Browse Computer InternetNotePad Downloading Playing Files Game
    4. 4. Music Player Playing Managing Songs Playlists Music Player Downloading Accessing Updates Internet
    5. 5. 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.
    6. 6. Thread – Life Cycle New Program starts thread Unlock signal Thread completes taskWaiting Runnable Terminated Await lock Await sleep Interval express Timed Waiting
    7. 7. Thread – Methods New Blocked t.sleep(),t.start() t.wait() Run method ends t.run() Ready Running Terminated
    8. 8. 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.
    9. 9. Creation of threads in multiple waysThreads can be created in two ways.• Implementing Runnable interface.• Extending Thread class
    10. 10. Implementing Runnable Interfacepackage 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(); } }
    11. 11. Extending Thread Classpackage 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(); } }
    12. 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. 13. Multithreading• When two or more threads are running in a process simultaneously then it is called Multithreading.
    14. 14. Multithreading Examplepackage 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"); } }
    15. 15. Multithreading Examplepackage 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(); }}
    16. 16. 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’.
    17. 17. How to synchronize the object? synchronized(object) { Synchronized //Statements } block synchronized returntype methodname() Synchronized { method //Statements }
    18. 18. 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()
    19. 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. 20. 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.
    21. 21. •Q& A..?
    22. 22. Thanks..!

    ×