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.


Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com