SlideShare ist ein Scribd-Unternehmen logo
1 von 39
ABHRA BASAK
APOORVA KUMAR
What will be covered?
• Concurrency and Parallelism in the Java Runtime Environment
• The Java Thread Model - Decisions for Design
• Implementing Multithreading – Creating and Managing Threads
• Remote Method Execution – Java RMI
• The Concurrency Utilities – java.util.concurrent
• Thread Safety – Shared Access to Data
• GUI Applications in Java
The Java Runtime
Environment
• Allows you to run the Java Compiler at the same time that
you are using an editor
Process-based multitasking
• A text editor can format text at the same time that it is
printing
Thread-based multitasking
Java
Libraries
Java
Code
Byte code
Byte code
Java Virtual Machine
Executable
Instructions
Decisions for Design
• Thread priorities are integers assigned by the Java runtime that
specify the relative priority of one thread to another.
• A thread can voluntarily relinquish control.
• A thread can be preempted by a higher priority thread.
• An elegant twist to an age-old model of inter-process
synchronization – the monitor.
• Most multithreaded systems expose monitors as objects that the
program must explicitly acquire and manipulate.
• Java provides a cleaner solution : each object has its own implicit
monitor, automatically called while invoking synchronized methods.
Thread 1 Thread 2
Creating and Managing
Threads
Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
• Thread execution
• start() – Start a thread by calling its run() method
• run() – Entry point for the thread
• Thread blocking
• sleep() – Suspend a thread for a period of time
• yield() – Voluntarily relinquish thread control to another thread of
the same priority
• Thread lifetime and termination
• isAlive() – Determine if a thread is still running
• join() – Wait for a thread to terminate
• Thread communication
• wait() – Instructs the calling thread to give up the monitor and
sleep until some other thread enters the same monitor and calls
notify()
• notify() – Wakes up a thread that called wait() on the same object
• notifyAll() – Wakes up all threads that called wait() on the same
object
Remote Method Invocation
• The RMI facility allows you to invoke operations on objects running in
other JVMs.
• When a remote method is called, the RMI stub, created by the RMI
compiler, packages the method parameters and sends them over the
network.
• The remote system unpacks the arguments and then calls the remote
method.
The Concurrent API
Packages
• Concurrency APIs
java.util.concurrent
• Atomic data types
java.util.concurrent.atomic
• Locks for synchronization
java.util.concurrent.locks
• Implements the classic semaphore
Semaphore
• Waits until a specified number of events have occurred
CountDownLatch
• Enables a group of threads to wait at a predefined execution point
CyclicBarrier
• Exchanges data between two threads
Exchanger
Executor
ExecutorService
ThreadPool
Executor
ScheduledPool
Executor
Future
Callable
Concurrent
HashMap
Concurrent
LinkedQueue
CopyOnWrite
ArrayList
Array
BlockingQueue
Concurrent
SkipListMap
Concurrent
SkipListSet
CopyOnWrite
ArraySet
DelayQueue
LinkedBlocking
Deque
LinkedBlocking
Queue
Prioirty
BlockingQueue
Synchronous
Queue
TimeUnit
Enumeration
DAYS
HOURS
MINUTES
SECONDS
MICRO
SECONDS
MILLI
SECONDS
NANO
SECONDS
Lock
ReentrantLock
ReadWriteLock
Reentrant
ReadWriteLock
AtomicInteger
AtomicLong
decrementAndGet() compareAndSet()
getAndSet()
Sharing Access to Data
Thread
safe
Behaves
correctly
(unambiguous)
Independent of
scheduling and
interleaving
Without
additional
synchronization
synchronized
• The primary mechanism for
synchronization
volatile
• Enables the thread to bypass the cache
when accessing the data
Multithreaded GUIs
• GUI systems use a single thread called the Event Dispatch Thread for
handling GUI events
• Multithreaded GUIs tend to be particularly prone to deadlocks.
• Swing is single-threaded particularly because of observations of
AWT which tried to be provide a higher degree of multi threaded
access.
• Longer running tasks triggered by event listeners in the even
dispatch thread are switched over to different threads to ensure the
GUI doesn’t freeze.
What we looked at
• Concurrency and Parallelism in the Java Runtime Environment
• The Java Thread Model - Decisions for Design
• Implementing Multithreading – Creating and Managing Threads
• Remote Method Execution – Java RMI
• The Concurrency Utilities – java.util.concurrent
• Thread Safety – Shared Access to Data
• GUI Applications in Java
• Android – A Java based mobile application platform
• Java Swing, AWT and Applets
• Process scheduling in Java Operating Systems
Time for Questions and
Comments
• Tim Peierls, Brian Goetz, Joshua Bloch, Joseph Bowbeer, Doug Lea, and
David Holmes. 2005. Java Concurrency in Practice. Addison-Wesley
Professional.
• Kazuaki Ishizaki, Shahrokh Daijavad, and Toshio Nakatani. 2011.
Refactoring Java programs using concurrent libraries. In Proceedings of the
Workshop on Parallel and Distributed Systems: Testing, Analysis, and
Debugging (PADTAD '11)
• Dan Grossman and Ruth E. Anderson. 2012. Introducing parallelism and
concurrency in the data structures course. In Proceedings of the 43rd ACM
technical symposium on Computer Science Education (SIGCSE '12)
• Alan D. Fekete. 2008. Teaching students to develop thread-safe java classes.
In Proceedings of the 13th annual conference on Innovation and technology in
computer science education (ITiCSE '08)
• Doug Lea. Concurrent Programming in Java: Design principles and patterns,
Addison-Wesley. First edition, October 1996 (also, German, Chinese, and
Japanese translations, and an on-line supplement). Second edition,
November 1999.
• Brian Goetz, with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes,
Doug Lea, Java Concurrency in Practice, Addison Wesley, 2006.

Weitere ähnliche Inhalte

Was ist angesagt?

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkArun Mehra
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introductionteach4uin
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 

Was ist angesagt? (19)

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Java threads
Java threadsJava threads
Java threads
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 

Andere mochten auch

09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprogramsbaran19901990
 
Parallel Programming In Modern World .NET Technics
Parallel Programming In Modern World .NET TechnicsParallel Programming In Modern World .NET Technics
Parallel Programming In Modern World .NET TechnicsIT Weekend
 
Legend of Java Concurrency/Parallelism
Legend of Java Concurrency/ParallelismLegend of Java Concurrency/Parallelism
Legend of Java Concurrency/ParallelismYuichi Sakuraba
 
Programming language
Programming languageProgramming language
Programming languageLia Safitri
 
Programing language
Programing languagePrograming language
Programing languageJames Taylor
 
SD & D Types of programming language
SD & D Types of programming languageSD & D Types of programming language
SD & D Types of programming languageForrester High School
 
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...Gerke Max Preussner
 
Modeling and abstraction, software development process [Software Modeling] [C...
Modeling and abstraction, software development process [Software Modeling] [C...Modeling and abstraction, software development process [Software Modeling] [C...
Modeling and abstraction, software development process [Software Modeling] [C...Ivano Malavolta
 
Programming languages
Programming languagesProgramming languages
Programming languagesAsmasum
 
JSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the WebJSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the WebKazuho Oku
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 

Andere mochten auch (19)

09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 
Parallel Programming In Modern World .NET Technics
Parallel Programming In Modern World .NET TechnicsParallel Programming In Modern World .NET Technics
Parallel Programming In Modern World .NET Technics
 
Introduction to programing languages part 2
Introduction to programing languages   part 2Introduction to programing languages   part 2
Introduction to programing languages part 2
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Introduction to programing languages part 1
Introduction to programing languages   part 1Introduction to programing languages   part 1
Introduction to programing languages part 1
 
295 - Abstraction
295 - Abstraction295 - Abstraction
295 - Abstraction
 
Sample Slides 2
Sample Slides 2Sample Slides 2
Sample Slides 2
 
Legend of Java Concurrency/Parallelism
Legend of Java Concurrency/ParallelismLegend of Java Concurrency/Parallelism
Legend of Java Concurrency/Parallelism
 
Programming language
Programming languageProgramming language
Programming language
 
Programing Language
Programing LanguagePrograming Language
Programing Language
 
Programing language
Programing languagePrograming language
Programing language
 
SD & D Types of programming language
SD & D Types of programming languageSD & D Types of programming language
SD & D Types of programming language
 
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
East Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
 
SD & D High and low level languages
SD & D High and low level languagesSD & D High and low level languages
SD & D High and low level languages
 
Abstraction
AbstractionAbstraction
Abstraction
 
Modeling and abstraction, software development process [Software Modeling] [C...
Modeling and abstraction, software development process [Software Modeling] [C...Modeling and abstraction, software development process [Software Modeling] [C...
Modeling and abstraction, software development process [Software Modeling] [C...
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
JSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the WebJSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the Web
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 

Ähnlich wie Concurrency in java

Java buzzwords
Java buzzwordsJava buzzwords
Java buzzwordsramesh517
 
Introduction to java by priti sajja
Introduction to java by priti sajjaIntroduction to java by priti sajja
Introduction to java by priti sajjaPriti Srinivas Sajja
 
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf10322210023
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingSachin Gowda
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdprat0ham
 
Indic threads pune12-typesafe stack software development on the jvm
Indic threads pune12-typesafe stack software development on the jvmIndic threads pune12-typesafe stack software development on the jvm
Indic threads pune12-typesafe stack software development on the jvmIndicThreads
 
PresentationPatterns_v2
PresentationPatterns_v2PresentationPatterns_v2
PresentationPatterns_v2Maksym Tolstik
 
JAVA object oriented programming (oop).ppt
JAVA object oriented programming (oop).pptJAVA object oriented programming (oop).ppt
JAVA object oriented programming (oop).pptAliyaJav
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptxmadan r
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to JavaSoumya Suman
 
Developing distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterKonstantin Tsykulenko
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introductionjyoti_lakhani
 

Ähnlich wie Concurrency in java (20)

Java buzzwords
Java buzzwordsJava buzzwords
Java buzzwords
 
Introduction to java by priti sajja
Introduction to java by priti sajjaIntroduction to java by priti sajja
Introduction to java by priti sajja
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
1. JAVA_Module_1-edited - AJIN ABRAHAM.pptx.pdf
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computing
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
 
Indic threads pune12-typesafe stack software development on the jvm
Indic threads pune12-typesafe stack software development on the jvmIndic threads pune12-typesafe stack software development on the jvm
Indic threads pune12-typesafe stack software development on the jvm
 
PresentationPatterns_v2
PresentationPatterns_v2PresentationPatterns_v2
PresentationPatterns_v2
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
4 threads
4 threads4 threads
4 threads
 
JAVA object oriented programming (oop).ppt
JAVA object oriented programming (oop).pptJAVA object oriented programming (oop).ppt
JAVA object oriented programming (oop).ppt
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Road Trip To Component
Road Trip To ComponentRoad Trip To Component
Road Trip To Component
 
Developing distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka Cluster
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
 

Mehr von Abhra Basak

FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...
FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...
FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...Abhra Basak
 
Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...
Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...
Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...Abhra Basak
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLAbhra Basak
 
Spanner - Google distributed database
Spanner - Google distributed databaseSpanner - Google distributed database
Spanner - Google distributed databaseAbhra Basak
 
DADAGIRI - The Fire Within
DADAGIRI - The Fire WithinDADAGIRI - The Fire Within
DADAGIRI - The Fire WithinAbhra Basak
 
Usability evaluation of the IIT Mandi Website
Usability evaluation of the IIT Mandi WebsiteUsability evaluation of the IIT Mandi Website
Usability evaluation of the IIT Mandi WebsiteAbhra Basak
 
Course Recommender
Course RecommenderCourse Recommender
Course RecommenderAbhra Basak
 
National Stock Exchange and Nasdaq 100
National Stock Exchange and Nasdaq 100National Stock Exchange and Nasdaq 100
National Stock Exchange and Nasdaq 100Abhra Basak
 

Mehr von Abhra Basak (8)

FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...
FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...
FINANCIAL MARKET PREDICTION AND PORTFOLIO OPTIMIZATION USING FUZZY DECISION T...
 
Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...
Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...
Privacy Preservation Issues in Association Rule Mining in Horizontally Partit...
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Spanner - Google distributed database
Spanner - Google distributed databaseSpanner - Google distributed database
Spanner - Google distributed database
 
DADAGIRI - The Fire Within
DADAGIRI - The Fire WithinDADAGIRI - The Fire Within
DADAGIRI - The Fire Within
 
Usability evaluation of the IIT Mandi Website
Usability evaluation of the IIT Mandi WebsiteUsability evaluation of the IIT Mandi Website
Usability evaluation of the IIT Mandi Website
 
Course Recommender
Course RecommenderCourse Recommender
Course Recommender
 
National Stock Exchange and Nasdaq 100
National Stock Exchange and Nasdaq 100National Stock Exchange and Nasdaq 100
National Stock Exchange and Nasdaq 100
 

Kürzlich hochgeladen

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Kürzlich hochgeladen (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 

Concurrency in java

Hinweis der Redaktion

  1. Multitasking – Thread-based and Process-basedProcess-based multitasking – Allows you to run the Java Compiler at the same time that you are using an editorThread-based multitasking – A text editor can format text at the same time that it is printing
  2. The Java Code and the Java libraries are together compiled to byte code.
  3. The byte code is then interpreted in the Java Virtual Machine (JVM) as executable instructions.Unlike many computer languages, Java provides built-in support for multithreading.All Java programs are multi-threaded (Inherent concurrency)The Garbage Collector always runs as a separate thread in the background.The Java runtime system depends upon threads for many things, and all the class libraries are designed with multithreading in mind.
  4. Single Threaded systems use an approach called an event loop with polling.In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next.Until an event handler returns, nothing else can happen in the system. This wastes CPU time.It can also result in one part of a program dominating the system, and preventing any other events from being processed.When a thread blocks, i.e., suspends execution, because it is waiting for some other resource, the entire program stops running.The benefit of Java’s multithreading is that the main loop / polling mechanism is eliminated.When a thread blocks in a Java program, only the single thread that is blocked pauses. All other threads continue to run.
  5. As an absolute value, a priority is meaningless. A higher priority thread doesn’t run any faster than a lower priority thread if it is the only thread running.Instead a thread’s priority is used to decide when to switch from one thread to the next. This is called a context switch.Relinquish: This is done by explicitly yielding, sleeping, or blocking on pending I/O. In this scenario, all other threads are examined, and the highest priority thread that is ready to run is given the CPU.Pre-emption: In this case a lower priority thread that does not yield the processor is simply preempted – no matter what it is doing – by a higher priority thread. Basically, as soon as a higher priority thread wants to run, it does. This is called preemptive multitasking.
  6. If you want two threads to communicate and share a complicated data structure, you need some way to ensure that they don’t conflict with each other.You can think of a monitor as a very small box that can hold only one thread. Once a thread enters a monitor, all other threads must wait until the thread exits the monitor.In this way, the monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.Once a thread is inside a synchronised method, no other thread can call any other synchronised methods on the same object. This helps you write very clear and concise multithreaded code, because synchronisation support is built into the language.
  7. If you want two threads to communicate and share a complicated data structure, you need some way to ensure that they don’t conflict with each other.You can think of a monitor as a very small box that can hold only one thread. Once a thread enters a monitor, all other threads must wait until the thread exits the monitor.In this way, the monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.Once a thread is inside a synchronised method, no other thread can call any other synchronised methods on the same object. This helps you write very clear and concise multithreaded code, because synchronisation support is built into the language.
  8. The program needs to define how separate threads communicate with each other.Java provides a clean, low-cost way for two or more threads to talk to each other, via calls to predefined methods that all objects have.Java’s messaging system allows a thread to enter a synchronized method on an object, and then wait there until another thread explicitly notifies it to come out.
  9. Create an instance of a class that extends class Thread.This class must override the run() method, which is the entry point of the new thread.It must also call start() to begin execution of the new thread.
  10. Create a class that implements the interface Runnable.An object of class Thread is instantiated from within the class.The class should override the method run() which is the entry point for the new thread.
  11. Inside run(), the code constitutes the new thread. run() establishes the entry point for another concurrent thread of execution within the program. Thus thread will end when run() returns.After the new thread is created, it will not start running until its start() method is called, which is declared within Thread. In essence, start() executes a call to run().A thread can be paused / suspended for a period of time using sleep(). Another thread can interrupt a sleeping thread. The sleeping thread then throws an InterruptedException that has to be caught explicitly by the program.
  12. The byte code is then interpreted in the Java Virtual Machine (JVM) as executable instructions.
  13. As conceptually pure as Java’s original support for multithreading is, it is not ideal for all applications – especially those that make extensive use of multiple threads. For example the original multithreading support does not provide several high-level features, such as semaphores, thread pools, and execution managers, that facilitate the creation of intensive concurrent programs.JDK 5 added the concurrency utilities, also commonly referred to as the concurrent API.The concurrency utilities are contained in the java.util.concurrent package and its subpackages java.util.concurrent.atomic and java.util.concurrent.locksjava.util.concurrent defines the core features that support alternatives to the built-in approaches to synchronization and interthread communication.
  14. Offer high level ways of synchronizing the interactions between multiple threads. Collectively, they enable one to handle several formerly difficult synchronization situations with ease. Each synchronizer provides a solution to a specific type of synchronization problem. This enables each synchronizer to be optimized for its intended use.A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than 0, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource.Sometimes you will want a thread to wait until one or more events have occurred. A CountDownLatch is initially created with a count of the number of events that must occur before the latch is released.When a set of two or more threads must wait at a predetermined execution point until all threads in the set have reached that point, to handle such a situation the API supplies a CyclicBarrier class. It enables you to define a synchronization object that suspends until the specified number of threads has reached the barrier point.Most interesting of the synchronizer classes is the Exchanger. It is designed to simplify the exchange of data between two threads. The operation of an Exchanger is astoundingly simple. It simply waits until two separate threads call its exchange() method. When that occurs, it exchanges the data supplied by the threads. This mechanism is both elegant and easy to use. For example, one thread might prepare a buffer for receiving information over a network connection, another might fill that buffer with the information from the connection.
  15. They manage thread execution. At the top of the Executor hierarchy is the Executor interface, which is used to initiate a thread. It defines execute().The ExecutorService extends Executor and provides methods that manage execution. There are two implemetations of ExecutorService : ThreadPoolExecutor and ScheduledPoolExecutor. Related to Executors are Future and Callable interfaces. Callable defines a thread that defines a value. An application can use Callable objects to compute results that are then returned to the invoking thread. This is a powerful mechanism because it facilitates the coding of numerical computations in which partial reults are computed simultaneously. It could also be used to return status codes to indicate successful completion of threads. A callable task is executed by the ExecutorService, by calling its submit() method.A Future contains a value that is returned by a thread after it executes. Thus its value becomes defined in the future, when the thread terminates. It is a generic interface that represents a value that will be returned by a Callable object.
  16. The package defines several concurrent collection classes such as ConcurrentHashMap,ConcurrentLinkedQueue, CopyOnWriteArrayList. These offer concurrent alternatives to their related classes defined by the collections framework.
  17. TimeUnit is an enumeration that is used to specify the granularity of the timing. The arguments of this type generally indicate a timeout period.Caution: there is no guarantee that the system is capable of the specified resolution, even if any of these values is allowed to be specified.
  18. The java.util.concurrent.locks package provides support for locks, which are objects that offer an alternative to using “synchronized” to control access to a shared resource. Before accessing a shared resource, the lock that protects the resource is acquired. When access to the resource is complete, the lock is released. If a second thread attempts to acquire the lock when it is in use by another thread, the second thread will suspend until the lock is released.All locks implement the Lock interface. The methods defined by Lock are:lock(), unlock(), lockInterruptibly() – waits until the invoking lock can be acquired unless interrupted, newCondition() – returns a Condition object that is associated with the invoking lock, tryLock() – attempts to acquire the lock, if unavailable, it will not wait, or will wait for no longer than the period specified.ReentrantLock implements Lock. It is a lock that can be repeatedly entered by the thread that currently holds the lock. (There should be an equal number of calls to unlock() to offset all calls to lock().) Otherwise, a thread seeking to acquire the lock will suspend until the lock is not in use.
  19. The java.util.concurrent.atomic package offers an alternative to the other synchronization features when reading or writing the values of some types of variables. This package offers methods that get, set, or compare the value of a variable in one uninterruptible atomic operation. This means that no lock or other synchronization mechanism is required.In general, the atomic operations offer a convenient, and possibly more efficient, alternative to the other synchronization mechanisms when only a single variable is involved.
  20. As classes and objects can be accessed by multiple threads when shared between them we need to ensure they are thread-safe.When is a class thread safe?It behaves correctly(ambiguous)Independent of thread scheduling and interleavingWithout additional synchronization
  21. Synchronized keyword: The primary mechanism for synchronization.Every object has an intrinsic lock or monitor associated with it. A thread needing exclusive and consistent access has to acquire the intrinsic lock.Every class also has such a lock associated with it for synchronizing access to static methods.Critical Sections of a program are marked with the synchronized keyword. Particularly useful for fine grained concurrencyVolatile keyword: This keyword enables the thread to bypass the cache when accessing the data.