SlideShare ist ein Scribd-Unternehmen logo
1 von 22
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..!

Weitere ähnliche Inhalte

Was ist angesagt?

Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 

Was ist angesagt? (20)

4
44
4
 
Django, What is it, Why is it cool?
Django, What is it, Why is it cool?Django, What is it, Why is it cool?
Django, What is it, Why is it cool?
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Action Research Design
Action Research DesignAction Research Design
Action Research Design
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Java operators
Java operatorsJava operators
Java operators
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
 
Behavioral Design Patterns
Behavioral Design PatternsBehavioral Design Patterns
Behavioral Design Patterns
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...
 
L11 array list
L11 array listL11 array list
L11 array list
 
Java logging
Java loggingJava logging
Java logging
 
Data Mining Tools / Orange
Data Mining Tools / OrangeData Mining Tools / Orange
Data Mining Tools / Orange
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality Reduction
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
 

Andere mochten auch

Java class 3
Java class 3Java class 3
Java class 3
Edureka!
 
Java class 7
Java class 7Java class 7
Java class 7
Edureka!
 
Java class 8
Java class 8Java class 8
Java class 8
Edureka!
 
Java class 1
Java class 1Java class 1
Java class 1
Edureka!
 
Java class 4
Java class 4Java class 4
Java class 4
Edureka!
 
Java class 5
Java class 5Java class 5
Java class 5
Edureka!
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 

Andere mochten auch (20)

Java class 3
Java class 3Java class 3
Java class 3
 
Java class 7
Java class 7Java class 7
Java class 7
 
Java class 8
Java class 8Java class 8
Java class 8
 
Java class 1
Java class 1Java class 1
Java class 1
 
Java class 4
Java class 4Java class 4
Java class 4
 
Java
Java Java
Java
 
Java class 5
Java class 5Java class 5
Java class 5
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data Structures
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with python
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 

Ähnlich wie Java class 6

07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 

Ähnlich wie Java class 6 (20)

econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
BCA MultiThreading.ppt
BCA MultiThreading.pptBCA MultiThreading.ppt
BCA MultiThreading.ppt
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Java Thread
Java ThreadJava Thread
Java Thread
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Mehr von Edureka!

Mehr von Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 

Kürzlich hochgeladen (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Java class 6

  • 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. 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 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. Thread – Life Cycle 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 • 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. Creation of threads in multiple ways Threads can be created in two ways. • Implementing Runnable interface. • Extending Thread class
  • 10. 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(); } }
  • 11. 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(); } }
  • 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 two or more threads are running in a process simultaneously then it is called Multithreading.
  • 14. 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"); } }
  • 15. 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(); } }
  • 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. How to synchronize the object? synchronized(object) { Synchronized //Statements } block synchronized returntype methodname() Synchronized { method //Statements }
  • 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. 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 • 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.

Hinweis der Redaktion

  1. Ready is not a state