Threads – An Introduction

Threads – An Introduction:
What is a Thread?
Thread in java means, sequential path followed by a program in execution. All the java programs have atleast one thread (main thread).

Defining a Thread:
There are two ways of defining a thread,

  • Extending java.lang.thread
  • Implementing Runnable Interface

When working with threads, it executes run method. We need to write the code that needs to be run in separate thread in run() method

Best Way for Thread:
It is always a good practice to Implement Runnable Interface. As Java does not support Multiple inheritance, If we extend thread class we cant extend any other class. So it is always better to implement Runnable.

How to Start a thread:
Even when we have code written in run() method for execution, We need Thread object to do the job for us. So in order to make thread start, We need to create thread object,

Extending thread class,
Thread thread = new Thread();

Implements Runnable,
Runnable runnable = new Runnable();
Thread thread = new Thread(runnable);

How to start multiple Threads?
We need to create multiple thread objects and use start() to start multiple threads, Here is a simple example

As you can see, We have started 3 threads, But the behavior is not same. Each time when you execute the code the thread output will differ.

Thread States:

  • New
  • Runnable
  • Running
  • Waiting/Sleeping/Blocked
  • Dead

When thread instance has been created but start() method is not been invoked.

When thread is eligible to run but it is not running. When start() method is invoked thread becomes Runnable state, also when thread comes back from sleep or waiting it will be in Runnable State.

When the thread is executing, it is in Running state.

This is the state when thread is not eligible to Run. The thread might be blocked, or waiting for some resource or developer might invoked sleep method

Thread is said to be dead when run() method completes. Once the thread is dead, we cant start that thread again. If we try to start the dead thread using start(), it will throw an exception

Thread Priorities:
We can set priorities for the thread for execution.
         Thread thread = new Thread();

Once we set priority, at any given time thread that runs will have the highest priority than other threads.

But Still…
When two or more threads have same priority, then the behavior is not guaranteed. It depends on the scheduler to decide which thread to run.

Yield ():
Yield is a Static method.  The work of yield() is to give equals chances to other threads with same priority. That is, It will make the running thread pushed to runnable state and gives chance to another thread with same priority. But that does not mean that Same thread might be get chosen again over others. The fact is yield does not do what it claims to do.

join is a non-static method. Join() allows one thread to join at the end of another thread. If Thread one cant execute until thread two is completed, Then thread one will join at the end of thread two. This causes the current thread to stop until the thread it joins completes.

Read More

String Operations, StringBuilder and StringBuffer

String Operations, StringBuilder and StringBuffer:
String is a non-primitive type in java. Strings are considered as Immutable. Before proceeding to example, Let us understand about String, StringBuilder and StringBuffer.

String is Immutable.

What is meant by Immutable?
In Java, Strings are treated as Objects. Immutable means once the object has been created it cannot be changed.

What are the advantages of String being Immutable?

  • It helps in Multi-threading, We can be sure that no one will change the string
  • Reduction in Memory usage

Two ways of Creating Strings:

  • String s1 = “StringValue”;
  • String S2 = new String(“StringValue2”);

Difference Between StringBuilder and StringBuffer:

StringBuilder StringBuffer
StringBuilder is mutable StringBuffer is mutable
StringBuilder is unsynchronized StringBuffer is Synchronized
StringBuilder is faster StringBuffer is slower when compared to StringBuilder

NOTE: StringBuffer and StringBuilder does not Override equals() and hashcode()

When to use String, StringBuffer and StringBuilder?
If our string is not going change, We use String.
If our string can change and accessed only from single thread, We can use StringBuilder.
If our string can change and can be accessed from multiple threads, We can use StringBuffer.




Read More

Java Custom Exceptions

Java Custom Exceptions: (Exceptions here)
In Java, developer can customize the exceptions. The developer has to write a class that extends Exception and can customize the exception.

Before we proceed further, Let us try to understand two keywords throw and throws

throw throws
throw is used to throw and exception from any method throws is used in method declaration
throw is used within a method throws is used with method signature
throw can be used only once within a method throws can declare more than once exception

Now let us see an example for Custom Exception,

In the above example, We have declared throws customException. We can declare multiple exceptions there. Example: Throws IO Exception, Arithmetic Exceptions etc. But we will be able to declare throw only once within a method.


Exceptions – Here

Read More

Exceptions – Checked and Runtime Exceptions with Examples

Exceptions – Checked and Runtime Exceptions with Examples: (Custom Exceptions here)
What is an Exception?
Exceptions are events that occurs during program execution.

What are the types of Exception?
There are two types of exceptions,

  • Checked Exception
  • Unchecked / Runtime Exceptions


What is the difference between Error and Exceptions?
Errors are subclass of Throwable. Errors are something which cannot be recovered and it is best practice not to try-catch the errors.
Example: OutofMemory Error

Exceptions are subclass of Throwable. Exceptions are something which can be recovered.
Example: IO Exception

Difference Between Checked Exceptions and Runtime Exceptions:

Checked Exceptions Unchecked Exceptions
Checked Exceptions can be caught and handled at Compile Time Unchecked Exceptions occurs at Runtime
Checked Exceptions are direct subclass of exceptions Unchecked Exceptions are subclass of Runtime Exceptions
Examples: IO Exception, Arithmetic exceptions etc. Examples: ArrayIndexOutofBound Exception, NullPointer exception etc

Example for Checked and Runtime Exceptions,


CustomExceptions – Here

Read More

Collections – List – ArrayList and LinkedList

Collections – List – ArrayList and LinkedList:
So far we have already discussed regarding Set and Map. In this article we are going to discuss regarding Lists. There are two types of Lists – ArrayList and LinkedList – (HashSet here and HashMap here)



Difference Between ArrayList and LinkedList: 

ArrayList LinkedList
ArrayList uses array to store the elements LinkedList uses doubly linked list to store the elements
ArrayList needs to know the size or it will re-create when it needs to grow LinkedList grows dynamically
ArrayList Manipulation is slow since it is an array LinkedList Manipulation is fast

When should we use ArrayList and When to use LinkedList:

  • When random access of elements are needed
  • If we know the size of array ahead so we can allocate the memory


  • When we need more insertions/deletions
  • When we do not know the size to be allocated

Example Program:


 HashSet here
HashMap here


Read More