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