Wednesday, 8 June 2016

Multithreading

Overview : 

Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.

This is especially important for the interactive, networked environment in which Java operates, because idle time is common.

For example, the transmission rate of data over a network is much slower than the rate at which the computer can process it

The Thread Class and the Runnable Interface:

n Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable.
n ·  To create a new thread, your program will either extend Thread or implement the Runnable interface.
n The Thread class defines several methods that help manage threads.

Method
Description
getName
Obtain a thread’s name.
getPriority
Obtain a thread’s priority.
isAlive
Determine if a thread is still running.
join
Wait for a thread to terminate.
run
Entry point for the thread.
sleep
Suspend a thread for a period of time.
start
Start a thread by calling its run method.
stop
stop a thread by calling its run method.

The Main Thread:

When a Java program starts up, one thread begins running immediately. This is usually called the main thread of your program, because it is the one that is executed when your program begins. The main thread is important for two reasons:
• It is the thread from which other “child” threads will be spawned.
• Often, it must be the last thread to finish execution because it performs various shutdown actions.

package com.vishal.multithreading;

public class ExampleOne {

   public static void main(String[] args) {

         Thread t = Thread.currentThread();
         System.out.println("Current Thread-:"+t);
         t.setName("My Thread");
         System.out.println("After Change in Name-:"+t);
            for(int i=5; i>0; i++){
                System.out.println("I Value-:"+i);
                  try {
                      Thread.sleep(1000);
                      } catch (InterruptedException e) {
                          System.out.println("Main thread interrupted");
                          e.printStackTrace();
                         }
                       }
                   }
                }

OutPut-:
Current Thread-:Thread[main,5,main]
After Change in Name-:Thread[My Thread,5,main]
I Value-:5
I Value-:6
I Value-:7
I Value-:8
I Value-:9

I Value-:10 it will keep on printing will only finish once memory leak is there.....

Creating a Thread..

(A) By extending Thread Class:

The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread. 

Here is the preceding program rewritten to extend Thread:

 package com.vishal.multithreading;

//create a Thread by extending thread class
public class NewThread extends Thread {

// create another thread
public NewThread() {
super("Demo Thread");
System.out.println("Child Thread:" + this);
// start the thread
start();
}

// This is entry point for the thread
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread:-:" + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child Interupted:");
}
System.out.println("Exsisting Child Thread....");
}

}


package com.vishal.multithreading;

public class FirstWayThreadClass {

public static void main(String args[]) {
// create a new thread
new NewThread();
try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread:-:" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main Interupted:");
}
System.out.println("Main thread Exsisiting.....");
}
}

Steps to refine output-:

1.  The child thread is created by instantiating an object of NewThread, which is derived from Thread.

2. Notice the call to super( ) inside NewThread. This invokes the following form of the Thread constructor:

public Thread(String threadName)
Here, threadName specifies the name of the thread


Monday, 6 June 2016

Java Inheritance

Java - Inheritance :
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
Extends Keyword:
extends is the keyword used to inherit the properties of a class.
class parent{
Sample Code.………
}

class child extends parent{
Sample Code.………
}
Example:
public class Calculation{ 
   int z;
   public void addition(int x, int y){
      z = x+y;
      System.out.println("The sum of the given numbers:"+z);
   }
   public void Subtraction(int x,int y){
      z = x-y;
      System.out.println("The difference between the given numbers:"+z);
   }
   
}

public class My_Calculation extends Calculation{    
  
   public void multiplication(int x, int y){
      z = x*y;
      System.out.println("The product of the given numbers:"+z);
   }
   public static void main(String args[]){
      int a = 20, b = 10;
      My_Calculation demo = new My_Calculation();
      demo.addition(a, b);
      demo.Subtraction(a, b);
      demo.multiplication(a, b);      
   }
}

Above program when an object to My_Calculation class is created, a copy of the contents of the super class is made with in it. That is why, using the object of the subclass you can access the members of a super class.
Inheritance

OOPs Basics


Object Oriented Programming is a paradigm that provides many concepts such as-:

· Inheritence
· Overridding
· Polymorphism
· Abstraction
· Encapsulation

OOPs Concept:


Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:

· Object
· Class
· Inheritance
· Polymorphism
· Abstraction
· Encapsulation

Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.

Class - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.

Inheritance - Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

Polymorphism - Polymorphism is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

Abstraction - Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words user will have the information on what the object does instead of how it does it.

Encapsulation - Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as as single unit.