Introduction to Multithreading /Chapter 1 / Multi Threading

Multi Threading

java multithreading


We can define java multithreading in the following two ways:-



  • By extending Thread class

  • By implementing Runable interface


1. By extending Thread class:-


example:-
class MyThread extends Thread
{
public void run ()//This for run method will be executed by child thread
{
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
}
class ThreadDemo
{
public static void main(String [] args)
{
MyThread t =new MyThread();

t.start();// starting of thread

for(int i=0;i<10;i++)
{
System.out.println("Main Thread");//This for Loop will be executed by main thread
}
}
}

run method:-


run method present is thread class and it has an empty implementation. child thread is responsible for executing run method. So we have to override the thread class run method in our java program. If we don't override run method then it's super class means thread class run method will be called and it has empty implementation so no output will be produced.

Java Customized Exceptions / Chapter 11 / Exception Handling

Java Customized Exceptions


Sometimes to fulfill the programming requirement we can define our own exceptions such type of exceptions are called customized exceptions.

Let's take an example of customized exceptions.
import java.util.*;
class TooYoungException extends RuntimeException
{
TooYoungException (String s)
{
super(s);
}
}
class TooOldException extends RuntimeException
{
TooOldException (String s)
{
super(s);
}
}
class CustomizeException
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age ");
int age=sc.nextInt();
if(age>60)
{
throw new TooOldException("Your age is already crossed the marriage age...
try In next birth... ");
}
else if(age<18)
{
throw new TooYoungException("Please wait some time...
You will get your best match soon... ");
}
else
{
System.out.println("Congrats!!!You are eligible for marriage..
you will get details of your match by email!!");
}
}
}

Output is:-

java throws keyword /Chapter 10 / Exception Handling

java throws

Java throws keyword


We can use throws keyword to delegate the responsibility of exception handling to the caller. It may be another method or JVM. After then caller method is responsible to handle that exception.

example:-
class Test
{
public static void main(String [] args) throws InterruptedException
{
Thread.sleep(5000);
}
}


java throw keyword /Chapter 9 /Exception Handling

java throw

java throw keyword:-


throw keyword used to create our own exception object explicitly and we can handover this (our own created) exception object to JVM manually.


example:-java throw

The main objective of throw keyword is to hand over our created exception object to JVM manually.


Let's take two example where the output will be same.

Some Possible Combinations of try catch finally /Chapter 8 / Exception Handling

try catch finally


Some possible combinations of try catch finally


try 
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(ArithmeticException e)
{

}

The above combination is invalid.And we will get Compile Time Error.

Difference Between final,finally,finalize /Chapter 7 / Exception Handling

Difference Between final, finally, finalize


final is a modifier applicable for method, classes & variables.


1.final method:-


Whatever method parent class has by default available to the child class through inheritance.

If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".


But,

If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.


2. final class:-

If a class declared as final we can't extend the functionality of that class so that we can not create the child class for that class. That's why inheritance is impossible for final classes.


try with multiple catch blocks / Chapter 6 / Exception Handling

try with multiple catch blocks


A way of handling an exception is varied from exception to exception hence, for every exception type, it is highly recommended to take separate catch block.

Let's take two examples of try catch.

Bad Or Worst Programming Practice
try 
{
Risky Code
}
catch (Exception e)
{
Exception Handling code
}

Best and Recommended Programming Practice

Method To Print Exception Information /Chapter 5 / Exception Handling

Method To Print Exception Information:-


The Throwable class defines the following methods to print Exception Information.Methods to print exception information

java try catch / Chapter 4 / Exception Handling

java try catch


java try catch block is highly recommended to handle exceptions.

The code which may rise an exception is called risky code and we have to define that code inside try block and corresponding handling code we have to define inside catch block.
try 
{
Risky Code
}
catch (Exception e)
{
Exception Handling code
}

let's take an example of a java program without try catch

Checked and Unchecked Exceptions /Chapter 3/ Exception Handling

Checked Exceptions


Checked Exceptions


The exceptions which are checked by the compiler for smooth execution of the program at runtime are called, checked exceptions.

example:-

FileNotFoundException,ServletException etc.

In our java program, if there is a possibility of rising checked exception then mandatory we should handle that checked exception (either by try catch or throws keyword) otherwise, we will get compile time error.

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com