Friday, January 24, 2020

Java Thread interrupt() VS interrupted() VS isInterrupted() Examples

1. Introduction


In this article, we will go through the thread class methods interrupt(), interrupted() and isInterrupted(). All these methods are looking for the same usage and context but they do different jobs in little differently.

Syntax:

public void interrupt()
public static boolean interrupted()
public boolean isInterrupted()

This method can be used to kill the thread using interrupt() method.

Java Thread interrupt() VS interrupted() VS isInterrupted() Examples


Wednesday, January 22, 2020

Matrix Multiplication with Java Threads - Optimized Code (Parallel)

1. Introduction


In this tutorial, We will write the code to matrix multiplication in java using the normal approach and multiple threads in parallel. This question will be asked in many interview program questions to see whether can you improve the performance for large matrixes.

We'll implement the programs for both cases.

Basic Matrix Multiplication Ref

Matrix 1 order = m x n (m rows and n columns)
Matrix 2 order = n x p (n rows and p columns)

Result matrix order = m x p (m rows and p columns)

Here, we are assuming input is given accurately.

Matrix Multiplication with Java Threads - Optimized Code (Parallel)


Monday, January 20, 2020

Java Thread States - Thread Life Cycle Flow - Multithreading Tutorial

1. Introduction


In this tutorial, We will see what is the life cycle of a Thread and what are the states involved in it. Typically, A thread can enter into any state by calling certain methods of thread. But, We can not see the thread state a particular time and but java builtin api has support to see the current status of running thread using getState() method.

In the previous article, we have seen the Thread concept of Thread Priorities.

Java Thread States - Thread Life Cycle Flow - Multithreading Tutorial

Friday, January 17, 2020

Java Thread characteristic Thread Priority - Multithreading

1. Introduction


In this article, We will understand one of two characteristics of Threads because those two are crucial for better analysis and debugging the issues. Here are the key factors of threads are Priority ad its State.

We have already explained how to create a thread using the Thread class and Runnable interface.

Remember, To run a java program one thread is required. When you are executing a program JVM will create a thread called Main thread that is the only thread created in normal and concurrent applications.

In java, Threads share all the resources of the application, including memory and open files. This is a powerful tool because they can share information in a fast and easy way but need to avoid race conditions using proper synchronization.

Java Thread characteristic Thread Priority  - Multithreading


Thursday, January 16, 2020

Creating Thread In Java - Multithreading Tutorial

1. Introduction


In this tutorial, We'll be learning how to create a thread in java. Before going to thread creation we should understand first the basic things about processors in our devices such as laptops and mobile smartphones.
Nowadays, we can use different applications at the same time. This can be working with Microsoft word document while listening to music or playing a game online. we can do all these things because we have an operating system that supports all of these.

Apart from different applications, we can do different works in one application. For instance, eclipse we can do write program and parallel we can run a search or building the application. Because our modern programming languages support to allow running multiple threads that run different tasks.

Coming to java, A Thread can be created in two ways as below.

A) Using Thread class
B) Using Runnable interface but we need to pass the object of this class to Thread class constructor.

Thread is a from java.lang package and Runnable is from java.util package.

Creating Thread In Java - Multithreading Tutorial


Wednesday, January 15, 2020

Java WeakHashMap Working Examples

1. Overview


In this tutorial, We'll be talking about WeakHashMap in java and it is placed in java.util package.

WeakHashMap is implemented based on Hashtable with weak keys. Map internally is built on Entry objects. When adding a key-value pair, it will be stored in the Entry object. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. So, once the key is deleted from the map it is eligible for Garbage Collection and we can not prevent from running GC on it. This class completely behaves differently than other Map implementations such as HashMap, LinkedHashMap, etc. And also it has efficiency parameters of initial capacity and load factor as same as HashMap.

Java WeakHashMap Working Examples


AbstractMap is the superclass for WeakHashMap and it implements Map interface. So, it acquires all its default methods. WeakHashMap is designed to store null values as key and value.

We should understand the basic terms Strong reference, Soft Reference, and Weak Reference.

Next in this article, We will see example programs on each method of this class.

Sunday, January 12, 2020

Java 8 Spliterator API - Working Examples

1. Overview

In this tutorial, We'll be talking about Spliterator introduced in the new Java 8 (JDK8). Spliterator is an interface that is designed for bulk or huge datasets and provides the best performance.

Spliterator takes the data from the source and traverses it. Finally, it divides the data into partitions. Source data can be array, any collection or IO Channel such as files.

Spliterator does the partition using trySplit() method as another Spliterator. By partitioning, Operations can be performed parallel.

A Spliterator also reports a set of characteristics() of its structure, source, and elements from among ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, and SUBSIZED. These may be employed by Spliterator clients to control, specialize or simplify computation.

For example, a Spliterator for a Collection would report SIZED, a Spliterator for a Set would report DISTINCT, and a Spliterator for a SortedSet would also report SORTED.

We will see its syntax and how Spliterator can be instantiated?
This has many useful methods and explanations for each method with example programs.

Java 8 Spliterator API - Working Examples


Friday, January 10, 2020

How to convert InputStream to File in Java

1. Overview


In this tutorial, We'll learn how to convert or write an InputStream to a File.

This can be done in different ways as below.

A) Using plain Java
B) Using java 7 NIO package
C) Apache Commons IO library
D) Guava

How to convert InputStream to File in Java



Wednesday, January 8, 2020

Java Program To Convert A Primitive Array To A List

1. Overview

In this programming tutorial, You will learn how to convert any primitive type array to a List. Primitive types are int, float, double, byte, long.
By end of this article, you will be able to convert int[] array to List<Integer> and float[] array to List<Float> or any primitive array to any List implementation.

Example programs are shown in classic java and using java 8 streams.

Java Program To Convert A Primitive Array To A List


Tuesday, January 7, 2020

Java 14 @Serial Annotation

1. Introduction


In this tutorial, we will learn a new annotation @Serial introduced in Java 14. This annotation is similar to the @Override annotation, @Serial annotation is used in combination with the serial lint flag to perform compile-time checks for the serialization-related members of a class.


Already this feature is available in build 25.

Java 14 @Serial Annotation

Monday, January 6, 2020

Creating A Simple ToDo Spring Web Application (Spring MVC)

1. Overview


In this tutorial, We'll learn how to create a simple ToDo web application using the Spring web framework. Spring web framework is called as officially Spring MVC. Let us create a simple REST api with CRUD operations such as Create, Read, Update, and Delete. You will learn and understand by a step by step tutorial. In the previous article, we have shown how to build a first Hello World application in Spring Boot and all possible real-time exceptions.

Spring 5.0.3.RELEASE
Bootstrap 3.3.7
Hibernate 4.3.11.Final
h2.version 1.4.197
JDK 1.8
Spring data 1.11.4.RELEASE


Sunday, January 5, 2020

Java Program: Read File Line By Line

1. Overview


In this programming tutorial, We'll learn today how to read a text file line by line. Java programs are shown for all possible ways to read file contents.
One of the file tutorials, we have already discussed in-depth on How to read large text files efficiently in java.

We will learn different approaches to read file line by line as String and pass this string to a separate method to process it.

Following are the java API class that provides a way to process the files.

Java Read File Line By Line


BufferedReader
Scanner
Files
RandomAccessFile

Saturday, January 4, 2020

How to Kill a Java Thread

1. Overview


In this tutorial, We will cover stopping a Thread in Java which is not that simple since the Thread.stop() method is deprecated. Because Thread.stop() method can lead to the monitored objects being corrupted. But, Still, many developers and applications are still using Thread.stop() method. Because they are not aware of its problems (monitored objects being corrupted.).

How to Kill a Java Thread Or How to stop a thread without using stop() method

let us see what are the other possible ways to stop the thread from its execution. This is a tricky topic for intermediate developers. Please read twice and practice the programs.

Wednesday, January 1, 2020

Java 8: Counting Matches on a Stream Filter - Stream.count(), Collectors.counting()

1. Overview


In this java 8 tutorial, We'll learn how to find the count of a Stream using Stream.count() and Collectors.counting() methods and also how to find the count that matches a specified condition or a Predicate. To use Predicate, we must use the filter() method from Stream API.
Let us start writing a few examples of finding count.

Java 8: Counting Matches on a Stream Filter


Java 8 Stream API filter() examples

1. Overview


In this Stream tutorial, We will learn today what is Stream filter introduced in Java 8. Java 8 has a Stream API and it has a filter() method which takes the Predicate Functional Interface as an argument. filter() method is a part of Intermediate Operations which is used in the middle of streams and returns a Stream as output.

Java 8 Stream filter() example