Showing posts with label Java 8. Show all posts
Showing posts with label Java 8. Show all posts

Sunday, December 8, 2019

Intellij Error: “Usage of API documented as @since 1.8+..”

Solution#1:

Add Java version in properies section of pom.xml
    <properties>
        ......
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
   
Solution#2:

You have to add build configuration in your pom.xml
    <build>
        .....
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

Solution#3: For IntelliJ users, add the following configuration.
Use both Solution#1 and Solution#2, Then add settings like below:

File -> Project Structure -> Project Settings -> Modules -> "Your Module Name" -> Sources -> Language Level

and change to your desired level i.e 1.8 or others.

Wednesday, March 7, 2018

Java 8 at a glance

runAsync() method:
=================
If you want to run some background task asynchronously and don’t want to return anything from the task, then you can use CompletableFuture.runAsync() method.

supplyAsync() method:
=====================
Run a task asynchronously and return the result using supplyAsync()

get() method:
=============
The CompletableFuture.get() method is blocking. It waits until the Future is completed and returns the result after its completion.

Example: String result = completableFuture.get()

CompletableFuture.complete() method:
===================================
CompletableFuture.complete() method is used to manually complete a Future.

Example: completableFuture.complete("Future's Result")

All the clients waiting for this Future will get the specified result. And, Subsequent calls to completableFuture.complete() will be ignored.

thenApply() method: [Will return]
===================
done sequentially after completion one by one. First one is completed first, then second one will start taking the first one's output.

thenApply() method is used to process and transform the result of a CompletableFuture when it arrives.

You can also write a sequence of transformations on the CompletableFuture by attaching a series of thenApply() callback methods. The result of one thenApply() method is passed to the next in the series


thenAccept() method: [Wouldn't return]
===================
thenRun() method:
=================

If you don’t want to return anything from your callback function and just want to run some piece of code after the completion of the Future, then you can use thenAccept() and thenRun() methods. These methods are consumers and are often used as the last callback in the callback chain.

CompletableFuture.thenAccept() takes a Consumer<T> and returns CompletableFuture<Void>

thenCompose() method:
=====================

thenCombine() method:
=====================

thenApplyAsync() method:
========================

CompletableFuture.allOf() method:
=================================

CompletableFuture.anyOf() method:
=================================


Difference between thenAccept() and thenRun():
==============================================
CompletableFuture.thenAccept() takes a Consumer<T> and returns CompletableFuture<Void>. It has access to the result of the CompletableFuture on which it is attached.

// thenAccept() example
CompletableFuture.supplyAsync(() -> {
return ProductService.getProductDetail(productId);
}).thenAccept(product -> {
System.out.println("Got product detail from remote service " + product.getName())
});
While thenAccept() has access to the result of the CompletableFuture on which it is attached, thenRun() doesn’t even have access to the Future’s result. It takes a Runnable and returns CompletableFuture<Void> -

// thenRun() example
CompletableFuture.supplyAsync(() -> {
    // Run some computation 
}).thenRun(() -> {
    // Computation Finished.
});

Resource Link: https://www.callicoder.com/java-8-completablefuture-tutorial/


Friday, December 1, 2017

java.lang.OutOfMemoryError: PermGen space Exception

What is java.lang.OutOfMemoryError: PermGen space?
Ans:
The java.lang.OutOfMemoryError: PermGen space message indicates that the Permanent Generation’s area in memory is exhausted.
Any Java applications is allowed to use a limited amount of memory. The exact amount of memory your particular application can use is specified during application startup.
Java memory is separated into different regions which can be seen in the following image: enter image description here
What will be the solution for this error PermGen space?
Ans:
For Heap,
export JVM_ARGS="-Xms1024m -Xmx1024m" 
For Permgen,
JVM_ARGS="-XX:PermSize=512M -XX:MaxPermSize=512m"
You can give also additional section
-XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC XX:+CMSClassUnloadingEnabled
You can also give more additional section :)
-XX:PermSize=512m -XX:MaxPermSize=512m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -XX:SurvivorRatio=2 -XX:MaxTenuringThreshold=128 -XX:TargetSurvivorRatio=90 -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing -XX:CMSIncrementalDutyCycleMin=0 -XX:CMSIncrementalDutyCycle=10 -XX:+UseTLAB -XX:+CMSParallelRemarkEnabled -XX:+CMSClassUnloadingEnabled 
Metaspace: A new memory space is born
The JDK 8 HotSpot JVM is now using native memory for the representation of class metadata and is called Metaspace; similar to the Oracle JRockit and IBM JVM's.
The good news is that it means no more java.lang.OutOfMemoryError: PermGen space problems and no need for you to tune and monitor this memory space anymore.
Related Link:
  1. https://plumbr.eu/outofmemoryerror/java-heap-space
  2. https://plumbr.eu/outofmemoryerror/permgen-space
  3. http://stas-blogspot.blogspot.in/2011/07/most-complete-list-of-xx-options-for.html#UseConcMarkSweepGC
  4. https://www.javacodegeeks.com/2013/12/decoding-java-lang-outofmemoryerror-permgen-space.html
  5. http://www.javavillage.in/reasons-outofmemory-permespace.php
  6. Dealing with "java.lang.OutOfMemoryError: PermGen space" error
Java 8 Links
  1. What is the use of MetaSpace in Java 8?
  2. https://blogs.oracle.com/poonam/entry/about_g1_garbage_collector_permanent
  3. https://plumbr.eu/outofmemoryerror/metaspace
  4. https://dzone.com/articles/java-8-permgen-metaspace

Sunday, November 19, 2017

Lombok does not work for Eclipse Neon. How to solve this issue?

In your eclipse.ini, add the following section
    -vmargs
    ...
    -javaagent:lombok.jar
    -Xbootclasspath/a:lombok.jar
It can solve your issue.
Antoniolazaro has given another solution for Eclipse Neon issue on MacOS
When I put the complete path, it works on Mac Os.
-javaagent:/Applications/Eclipse.app/Contents/Eclipse/lombok.jar

Resource Link:

  1. https://github.com/rzwitserloot/lombok/issues/1173
And the lombok project owner rzwitserloot has given 3 suggestions for checking
  1. Can you give us the complete path, from root, of your eclipse install? Starting with 1.16.14, the installer should still find eclipse even with the funky paths that the OOM installer comes up with. I have no idea why the installer isn't finding your eclipse automatically, here. I can't reproduce this on my mac.
  2. The lombok uninstaller has the same 'find eclipses' capability as the installer. Mac doesn't have a registry and we aren't going to dump a bunch of crud in your homedir just to keep track of where we installed your lombok; we like it that way: No cruft. So, that part (that the uninstaller can't find a custom-install location and requires you to again specify it) is not a bug.
  3. You still have to add lombok.jar to your project as a dependency same as any other third party dependency (something like guava, for example). It's not clear from your problem description if you actually did that.

Resource Link: