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.
29. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Executors
• A custom thread factory can be used to create an
executor
• Thread factory can configure the internal threads
ThreadFactory tf = . . .
Executor myExecutor = Executors.newCachedThreadPool(tf);
30. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Executors
public class MyThreadFactory implements ThreadFactory {
final AtomicLong counter = new AtomicLong(0);
@Override
public Thread newThread(Runnable r) {
final Thread thread = new Thread();
thread.setName("My private thread " + counter.incrementAndGet());
thread.setUncaughtExceptionHandler((t, e) -> {
System.out.println("BOOOOOOM!");
});
return thread;
}
}
43. Karakun DevHub_
@HendrikEbbersdev.karakun.com
synchronized
• The synchronized keyword provides thread
synchronization
• Internally Java provides a monitor lock
• Each monitor is bound to an object
• Java provides several ways how the
synchronized keyword can be used
52. Karakun DevHub_
@HendrikEbbersdev.karakun.com
synchronized
public class MyHolder extends Holder {
private final List<Integer> l = new ArrayList();
public synchronized void run() {
while(true) {
sleep(1000);
printTime();
}
}
}
Once this is called all
synchronised methods form
Holder class are useless…
54. Karakun DevHub_
@HendrikEbbersdev.karakun.com
synchronized
public class Holder {
private final Object monitor = new Object();
public void myMethod() {
synchronized(monitor) {
//TODO: add code
}
}
}
Internal Code of the block
is synchronized.
Lock is based on monitor
object / instance
57. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Lock
public class Holder {
private final List<String> listA;
private final Lock listLockA;
private final List<String> listB;
private final Lock listLockB;
}
Just define a lock instance
for each Monitor that
should be synchronized
59. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Lock
• A Lock controls access to a shared resource by
multiple threads
• The most important methods of a Lock are lock()
and unlock()
• This method defines exclusive access to a resource
60. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Lock
• By calling lock() the resource will be locked by the
current thread
• By calling unlock() the current thread can finish
the exclusive locking of the resource
• Calling lock() will block the current thread until no
other thread has an exclusive lock
67. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Lock
• Even more complexe use cases are supported by
the Lock API.
• Use tryLock() to not block threads.
• Create a Condition for monitor functionality.
Will be handled in a
separate topic
68. Karakun DevHub_
@HendrikEbbersdev.karakun.com
Other solutions
• By the way the complete problem could be handled
in a different way:
• Java provides some special collection types to get
rid of ConcurrentModificationException:
Will be handled in a
separate topic
private final List<String> listA = new ArrayList<>();
List<String> listA = new CopyOneWriteArrayList<>();
69. @HendrikEbbers
• Check our website for developers
• Subscribe to our newsletter
• Join us
dev.karakun.com | @HendrikEbbers
Karakun DevHub_
dev.karakun.com
https://dev.karakun.com
https://dev.karakun.com/subscribe/
https://dev.karakun.com/you-at-karakun/