Java Notes
-
Java Fork and Join using ForkJoinPool
The ForkJoinPool was added to Java in Java 7. The ForkJoinPool is similar to the Java ExecutorService but with one difference. The ForkJoinPool makes it easy for tasks to split their work up into…
-
Java 8 New Features
New Features There are dozens of features added to Java 8, the most significant ones are mentioned below − Lambda expression − Adds functional processing capability to Java. Method references −…
-
Java 7's new Features
There are a number of features in Java 7 that will please developers. Things such as strings in switch statements, multi-catch exception handling, try-with-resource statements, the new File System…
-
What is java.lang.OutOfMemoryError in Java ?
OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and JVM throws java.lang.OutOfMemoryError when it ran out of memory in the heap. OutOfMemoryError in Java can come anytime in…
-
Print strings in sequence with multiple threads
public class MTest { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new Runnable() { public void run() { for (int i = 0; i
-
Create DeadLock in Java
public class MyDeadlock { String str1 = "Java"; String str2 = "UNIX"; Thread trd1 = new Thread("My Thread 1"){ public void run(){ while(true){ synchronized(str1){ synchronized(str2){…
-
ThreadPoolExecutor
The java.util.concurrent.ThreadPoolExecutor is an implementation of the ExecutorService interface. The ThreadPoolExecutor executes the given task (Callable or Runnable) using one of its internally…
-
Life cycle of a Thread (Thread States)
A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state. But for better…
-
Java.lang.Thread.yield() Method
Description The java.lang.Thread.yield() method causes the currently executing thread object to temporarily pause and allow other threads to execute. Declaration Following is the declaration for…
-
Java.lang.Thread.join() Method
Description The java.lang.Thread.join() method waits for this thread to die. Declaration Following is the declaration for java.lang.Thread.join() method public final void join() throws…
-
What is the difference between preemptive scheduling and time slicing?
Preemptive scheduling: The highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Time slicing: A task executes for a predefined…
-
What is Inheritance in Java ?
Inheritance in Java or OOPS (Object oriented programming) is a feature which allows coding reusability. In other words, Inheritance self-implies inheriting or we can say acquiring something from…
-
Inheritance in Java- Things to remember
Here are some important points about Inheritance in Java which is worth remembering: One subclass can extend only one super class in Java but it can implement the multiple interfaces. A private…
-
Why multiple inheritances are not supported in Java ?
The diamond problem A diamond class inheritance diagram. The "diamond problem" (sometimes referred to as the "deadly diamond of death"[4]) is an ambiguity that arises when two classes B and C inherit…
-
How hashmap works in java ?
Single statement answer If anybody asks me to describe “How HashMap works?“, I simply answer: “On principle of Hashing“. As simple as it is. Now before answering it, one must be very sure to know at…
-
Working with hashCode and equals methods in java
The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer,…
-
Volatile Vs Atomic
The effect of the volatile keyword is approximately that each individual read or write operation on that variable is atomic. Notably, however, an operation that requires more than one read/write --…
-
How can I sort a LinkedHashMap based on its values given that the LinkedHashMap contains of String and Integer. So I need to sort it based on the Values which are Integers.
map.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .forEach(entry -> ... );
-
Immutable Objects
An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code.…
-
Difference between final, finally and finalize
Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed. Finally is used to place…
-
Callable and Future Example in Java
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import…
-
Java PriorityQueue Example
class Book implements Comparable { int id; String name,author,publisher; int quantity; public Book(int id, String name, String author, String publisher, int quantity) { this.id = id; this.name =…
-
What GCs were introduced in java 7,8?
Garbage Collection in Earlier versions Java 1.5 The major enhancements in Java 1.5 Garbage Collector (GC) has changed from the previous serial collector (-XX:+UseSerialGC) to a parallel collector…
-
Making Singleton thread safe
Thread Safety in Java Singleton Classes In general we follow below steps to create a singleton class: 1) Override the private constructor to avoid any new object creation with new operator. 2)…
-
Linkedlist vs Arraylist
Linkedlist vs Arraylist added at 8 July, 2018 LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList…