The document discusses Java concurrency concepts including locks, threads, atomics, and thread pools. It provides examples of using ReentrantLock for locking, AtomicLong for atomic counters, and ThreadPoolExecutor for managing threads. The document also mentions different Java concurrency implementations and creating a lock factory to choose the implementation.
Every Java developer knows that multithreading is the root of all evil and it is quite hard to write correct code for concurrent environment. But what tasks do exist in real commercial development except running code in asynchronous way?
In this talk I will present several tasks from my real projects and solutions we designed for them. This talk is very application oriented and allows participants to extend their vision of concurrent programming.
This talk was given in ilJUG on the 29th of July 2014 and discusses the new Java8 StampedLock class. It compares it to different locking mechanism is Java and shows some insights deduced from a simple benchmark
This presentation is about advanced multithreading and concurrency in Java. I have tried my best to explain the concepts with code. Feel free to reach me if you have any questions or concerns.
Every Java developer knows that multithreading is the root of all evil and it is quite hard to write correct code for concurrent environment. But what tasks do exist in real commercial development except running code in asynchronous way?
In this talk I will present several tasks from my real projects and solutions we designed for them. This talk is very application oriented and allows participants to extend their vision of concurrent programming.
This talk was given in ilJUG on the 29th of July 2014 and discusses the new Java8 StampedLock class. It compares it to different locking mechanism is Java and shows some insights deduced from a simple benchmark
This presentation is about advanced multithreading and concurrency in Java. I have tried my best to explain the concepts with code. Feel free to reach me if you have any questions or concerns.
Presentation describes basic concepts of thread pool such as:
- interacting with queues,
- using pools from Executors class,
- task rejecting
- using ThreadFactory for thread creating
- Future and Callable interfaces,
- basic API
- possibilities for extending
Advanced Introduction to Java Multi-Threading - Full (chok)choksheak
Designed for the beginning Java developer to grasp advanced Java multi-threading concepts quickly. Talks mainly about the Java Memory Model and the Concurrent Utilities. This presentation is Java-specific and we intentionally omit general non-Java-specific details, such as hardware architecture, OS, native threads, algorithms, and general software design principles etc.
Slide deck from my presentation on multi-threading with .NET. The presentation covers from beginner onwards and looks at current technologies (i.e. pre .NET 4.0) specifically.
What makes this extra special is the entire process of how I prepared for it, from finding content to slide deck layout to presentation prep is documented at: http://www.sadev.co.za/content/how-i-build-presentations-series-index
Java has a solid Memory Model, and there are a couple of excellent libraries for concurrency. When you start working with threads however, pitfalls start appearing - especially if the program is supposed to be fast and correct. This session shows proven solutions for some typical problems, showing how to view program code from a concurrency perspective: Which threads share which data, and how? How to reduce the impact of locks? How to avoid them altogether - and when is that worth it?
Lecture 10 from the IAG0040 Java course in TTÜ.
See the accompanying source code written during the lectures: https://github.com/angryziber/java-course
Multithreading is the ability of a program or an
operating system process to manage its use by
more than one user at a time and to even manage
multiple requests by the same user without
having to have multiple copies of the
programming running in the computer.
Presentation describes basic concepts of thread pool such as:
- interacting with queues,
- using pools from Executors class,
- task rejecting
- using ThreadFactory for thread creating
- Future and Callable interfaces,
- basic API
- possibilities for extending
Advanced Introduction to Java Multi-Threading - Full (chok)choksheak
Designed for the beginning Java developer to grasp advanced Java multi-threading concepts quickly. Talks mainly about the Java Memory Model and the Concurrent Utilities. This presentation is Java-specific and we intentionally omit general non-Java-specific details, such as hardware architecture, OS, native threads, algorithms, and general software design principles etc.
Slide deck from my presentation on multi-threading with .NET. The presentation covers from beginner onwards and looks at current technologies (i.e. pre .NET 4.0) specifically.
What makes this extra special is the entire process of how I prepared for it, from finding content to slide deck layout to presentation prep is documented at: http://www.sadev.co.za/content/how-i-build-presentations-series-index
Java has a solid Memory Model, and there are a couple of excellent libraries for concurrency. When you start working with threads however, pitfalls start appearing - especially if the program is supposed to be fast and correct. This session shows proven solutions for some typical problems, showing how to view program code from a concurrency perspective: Which threads share which data, and how? How to reduce the impact of locks? How to avoid them altogether - and when is that worth it?
Lecture 10 from the IAG0040 Java course in TTÜ.
See the accompanying source code written during the lectures: https://github.com/angryziber/java-course
Multithreading is the ability of a program or an
operating system process to manage its use by
more than one user at a time and to even manage
multiple requests by the same user without
having to have multiple copies of the
programming running in the computer.
Java APIs- The missing manual (concurrency)Hendrik Ebbers
This isn’t a talk about microservices, NoSQL, container solutions or hip new frameworks. This talk will show some of the standard Java APIs that are part of Java since version 5, 6, 7 or 8. All those features are very helpful to create maintainable and future-proof applications, regardless of whether JavaEE, Spring, JavaFX or any other framework is used. The talk will give an overview of some important standard concepts and APIs of Java like annotations, null values and concurrency.
Multithreading and concurrency in androidRakesh Jha
Here you will learn -
What is Multithreading
What is concurrency
Process Vs Thread
Improvements and issues with concurrency
Limits of concurrency gains
Concurrency issues
Threads pools with the Executor Framework
AsyncTask and the UI Thread
Code
Threads : Single and Multitasking, Creating and terminating the thread, Single and Multi tasking
using threads, Deadlock of threads, Thread communication.
4. What is Java Concurrency ?
Concurrency means existing or in operation at the same time
or together.
Java provides :
1. Lock
2. Thread and Thread Management
3. Atomic
It is in the package java.util.concurrency
7. What are the locks in Java?
Lock
Lock implementations provide more extensive locking operations
than can be obtained using synchronized methods and statements.
They allow more flexible structuring, may have quite different
properties, and may support multiple associated Condition
objects.
ReadWriteLock
A ReadWriteLock maintains a pair of associated locks, one for
read-only operations and one for writing. The read lock may be
held simultaneously by multiple reader threads, so long as there
are no writers. The write lock is exclusive.
8. Lock
A lock is a tool for controlling access to a shared resource by
multiple threads. Commonly, a lock provides exclusive
access to a shared resource: only one thread at a time can
acquire the lock and all access to the shared resource requires
that the lock be acquired first. However, some locks may
allow concurrent access to a shared resource, such as the
read lock of a ReadWriteLock.
9. ReentrantLock
A reentrant mutual exclusion Lock with the same basic
behavior and semantics as the implicit monitor lock accessed
using synchronized methods and statements, but with
extended capabilities.
10. Demo
ReentrantLock reentrantLock=new ReentrantLock();
public int getNextSequenceID()
{
return getNewValue();
}
Lock acquired
private int getNewValue()
{
reentrantLock.lock();
value+=1;
reentrantLock.unlock();
return value; Lock unacquired
}
12. Creating a Lock Factory with both
implementations
Check to see if edu.emory.mathcs.backport implementation
is available
static {
try {
Class.forName("edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock");
backportConcurrentPresent = true;
} catch (ClassNotFoundException ex) {
backportConcurrentPresent = false;
}
}
13. Creating a Lock Implementation
if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_15) {
return new JdkConcurrentLock(timeoutSeconds);
} else if (backportConcurrentPresent) {
return new BackportConcurrentLock(timeoutSeconds);
15. Uses
It can be
use as an atomic update flag (AtomicBoolean)
use as a sequential atomic counter (AtomicInteger)
use as an atomically incremented sequence
number(AtomicLong)
use to hold an atomic reference to an object
(AtomicReference<V>,V is the reference object)
16. Demo
private AtomicLong atomic;
public AtomicTest(long intialValue)
{
atomic=new AtomicLong(intialValue);
}
public Long increment()
{
return atomic.incrementAndGet();
}
18. What to look at?
ThreadFactory
ThreadPoolExecutor
Callable (special form of Runnable class)
19. ThreadFactory
Implement a custom thread factory
public class CustomThreadFactory implements ThreadFactory
There is a single method called newThread that takes
Runnable as its parameter
Purpose : To create a new thread for the
ThreadPoolExecutor when a new work requires a thread.
20. How to create a custom
ThreadFactory?
public class CustomThreadFactory extends CustomThreadCreator implements
ThreadFactory{
public CustomThreadFactory { super(); }
public CustomThreadFactory(String threadNamePrefix) {
super(threadNamePrefix); }
public Thread newThread(Runnable r) {
return createThread(r);
}
}
21. ThreadPoolExecutor
It can be schedule or not
For scheduled thread executor
use :ScheduledThreadPoolExecutor class.
A custom thread pool executor may be preferable.
23. Callable
This just like Runnable but this returns a result of the
execution task.
Call the submit method of ThreadPoolExecutor instance and
pass in the Callable interface implementation.