• Complain

Fu Cheng [Fu Cheng] - Exploring Java 9: Build Modularized Applications in Java

Here you can read online Fu Cheng [Fu Cheng] - Exploring Java 9: Build Modularized Applications in Java full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Fu Cheng [Fu Cheng] Exploring Java 9: Build Modularized Applications in Java
  • Book:
    Exploring Java 9: Build Modularized Applications in Java
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2017
  • Rating:
    5 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 100
    • 1
    • 2
    • 3
    • 4
    • 5

Exploring Java 9: Build Modularized Applications in Java: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Exploring Java 9: Build Modularized Applications in Java" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Discover all the new features and changes in Java 9, including module systemsJPMS or Project Jigsaw. This book covers the whole Java application development life cycle. Youll review all the important concepts, including module descriptor, unnamed module, automatic module, and command line tools.
Exploring Java 9 also serves as a practical guide for migration to module systems. Code samples from real-world scenarios solidify a foundation for learning and development and allow you to apply best practices in actual development.
Additionally, youll learn about concurrency, ECMAScript 6 features in Nashorn and Parser API, stack-walking API, Stream and Optional, utilities classes, and I/O. And its now possible to build modularized applications in Java. Youll see how JPMS affects not only the JDK itself, but also applications that are developed upon it.
What Youll Learn
Build modularized applications in Java Migrate to module systems Master enhanced method handles

Who This Book Is ForJava developers with basic development skills

Fu Cheng [Fu Cheng]: author's other books


Who wrote Exploring Java 9: Build Modularized Applications in Java? Find out the surname, the name of the author of the book and a list of all author's works by series.

Exploring Java 9: Build Modularized Applications in Java — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Exploring Java 9: Build Modularized Applications in Java" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Fu Cheng 2018
Fu Cheng Exploring Java 9
10. Concurrency
Fu Cheng 1
(1)
Auckland, New Zealand
This chapter summarizes changes related to concurrency in Java 9.
CompletableFuture
Several new methods have been added to java.util.concurrent.CompletableFuture in Java 9.
Async
The CompletableFuture completeAsync(Supplier extends T> supplier, Executor executor) and CompletableFuture completeAsync(Supplier extends T> supplier) methods c omplete the CompletableFuture by using an asynchronous task to invoke the Supplier to get the result. The task is executed using the provided executor or the default executor. Listing .
public class TimeUnitTest {
@Test
public void testChronoUnit() throws Exception {
assertEquals(TimeUnit.MINUTES, TimeUnit.of(ChronoUnit.MINUTES));
assertEquals(ChronoUnit.SECONDS, TimeUnit.SECONDS.toChronoUnit());
}
}
Listing 10-4.
Example of TimeUnit and ChronoUnit
Queues
The new methods forEach(Consumer super E> action), removeAll(Collection> c) , removeIf(Predicate super E> filter) , and retainAll(Collection> c) have been added to the following classes:
  • java.util.concurrent.ArrayBlockingQueue
  • java.util.concurrent.ConcurrentLinkedDeque
  • java.util.concurrent.ConcurrentLinkedQueue
  • java.util.concurrent.LinkedBlockingDeque
  • java.util.concurrent.LinkedBlockingQueue
  • java.util.concurrent.LinkedTransferQueue
Listing shows some examples of how to use these new methods in different queue implementations.
public class QueueTest {
@Test
public void testForEach() {
final ArrayBlockingQueue queue = new ArrayBlockingQueue<>(3);
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.forEach(System.out::println);
}
@Test
public void testRemoveAll() {
final LinkedBlockingQueue queue = new LinkedBlockingQueue<>(3);
queue.offer(1);
queue.offer(2);
queue.removeAll(List.of(1));
assertEquals(1, queue.size());
}
@Test
public void testRemoveIf() {
final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.removeIf(i -> i % 2 == 0);
assertEquals(2, queue.size());
}
@Test
public void testRetainAll() {
final LinkedBlockingDeque deque = new LinkedBlockingDeque(3);
deque.offer(1);
deque.offer(2);
deque.offer(3);
deque.retainAll(List.of(1));
assertEquals(1, deque.size());
}
}
Listing 10-5.
Examples of New Methods in Queues
Atomic Classes
Those new methods in the class VarHandle related to the memory access modes are also added to existing atomic classes in the package java.util.concurrent.atomic , including AtomicBoolean , AtomicInteger , AtomicIntegerArray , AtomicLong , AtomicLongArray , AtomicReference , and AtomicReferenceArray . These methods have the same memory semantics as the corresponding methods in VarHandle .
  • compareAndExchange()
  • compareAndExchangeAcquire()
  • compareAndExchangeRelease()
  • weakCompareAndSetVolatile()
  • weakCompareAndSetAcquire()
  • weakCompareAndSetRelease()
  • weakCompareAndSetPlain()
  • getAcquire()
  • getOpaque()
  • getPlain()
  • setOpaque()
  • setPlain()
  • setRelease()
Listing shows some examples of how to use these new methods.
public class AtomicTest {
@Test
public void testGetAcquire() {
final AtomicBoolean value = new AtomicBoolean();
value.setRelease(false);
assertEquals(false, value.getAcquire());
}
@Test
public void testCompareAndExchange() {
final AtomicInteger value = new AtomicInteger(10);
final int returned = value.compareAndExchange(10, 5);
assertEquals(10, returned);
assertEquals(5, value.getPlain());
}
}
Listing 10-6.
Examples of New Methods in Atomic Classes
Thread.onSpinWait
When youre using a thread to run a task, its common to have the task wait for certain condition before it can continue. You usually do this by using a volatile variable as the flag. This flag is set by another thread to stop the waiting. Before Java 9, you used an empty loop to wait for the condition. The new static method onSpinWait() of class Thread in Java 9 can make the waiting more efficient.
Listing shows an example of Thread.onSpinWait() . Classes NormalTask and SpinWaitTask both use the volatile Boolean variable canStart as the flag. The flag is set to true using the method start() . The different between these two classes is that SpinWaitTask uses Thread.onSpinWait() in the loop. Here I use another thread to call the method start() after three seconds to stop the waiting.
public class ThreadOnSpinWait {
public static void main(final String[] args) throws InterruptedException {
final NormalTask task1 = new NormalTask();
final SpinWaitTask task2 = new SpinWaitTask();
final Thread thread1 = new Thread(task1);
thread1.start();
final Thread thread2 = new Thread(task2);
thread2.start();
new Thread(() -> {
try {
Thread.sleep(3000);
} catch (final InterruptedException e) {
e.printStackTrace();
} finally {
task1.start();
task2.start();
}
}).start();
thread1.join();
thread2.join();
}
private abstract static class Task implements Runnable {
volatile boolean canStart;
void start() {
this.canStart = true;
}
}
private static class NormalTask extends Task {
@Override
public void run() {
while (!this.canStart) {
}
System.out.println("Done!");
}
}
private static class SpinWaitTask extends Task {
@Override
public void run() {
while (!this.canStart) {
Thread.onSpinWait();
}
System.out.println("Done!");
}
}
}
Listing 10-7.
Example of Thread.onSpinWait()
Summary
In this chapter, we discussed changes related to concurrency in Java 9, including asynchronous task execution and timeout support in CompletableFuture , new methods added to queues and atomic classes, and the new method Thread.onSpinWait() . In the next chapter, well discuss changes in the JavaScript engine Nashorn.
Fu Cheng 2018
Fu Cheng Exploring Java 9
11. Nashorn
Fu Cheng 1
(1)
Auckland, New Zealand
Nashorn ( http://openjdk.java.net/projects/nashorn/ ) is the JavaScript engine that was introduced in Java 8 to replace the old engine based on Mozilla Rhino. In Java 8 Nashorn was based on ECMAScript 5. Some new features of ECMAScript 6 have already been implemented in the Nashorn engine in Java 9. The Nashorn-related API is in the module jdk.scripting.nashorn .
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Exploring Java 9: Build Modularized Applications in Java»

Look at similar books to Exploring Java 9: Build Modularized Applications in Java. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Exploring Java 9: Build Modularized Applications in Java»

Discussion, reviews of the book Exploring Java 9: Build Modularized Applications in Java and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.