JAVA part-4

Document Sample
JAVA part-4 Powered By Docstoc
					UNIT-4
 Concepts of Exception handling
 Exception hierarchy
 Types of exceptions
 Usage of try, catch, throw, throws, finally keywords
 Built in Exceptions
 Creating own Exception sub classes


 Concepts of multi threading
 Types of multi tasking, uses of multitasking
 Thread life cycle
 Creating multiple threads by using thread class
 Creating multiple threads by using Runnable interface
 Synchronization
 Thread life cycle
                            Program Errors

 There are basically 3 types of errors in a java program:

            1.Compile-time errors

            2.Run-time errors

            3.Logical errors.
1.Compile-time errors:
     These are syntactical errors found in the code, due to which a program fails to
      compile.
     For example, forgetting a semicolon at the end of the a java program, or writing a
      statement without proper syntax will result in compilation-time error.


    class CError{
         public static void main(String args[]){
             System.out.println(“Compilation Error…..”)
         }
    }



                       D:\>javac Cerror.java
                       CError.java:4: ';' expected
                            }
                            ^
                       1 error
2.Run-time errors:
     These are the errors which represent inefficiency of the computer system to execute
      a particular statement, means computer system can not process.
     For example, division by zero error occur at run-time.


    class RError{
         public static void main(String args[]){
             int a=10,b=0;
             System.out.println(“a/b: ”+(a/b));
         }
    }


   D:\>javac RError.java

   D:\>java RError
   Exception in thread "main" java.lang.ArithmeticException: / by zero
        at RError.main(RError.java:4)
3.Logical errors:
     These errors depict flaws in the logic of the program.
     The programmer might be using a wrong formula or the design of the program itself
      is wrong.
     Logical errors are not detected either by the java compiler or JVM.
     The programmer is solely responsible for them.
     Logical errors means the logic of the program is wrong and these are identified after
      observing the output.

   class LError{
         int sum(int a,int b){
                  return a-b;
         }
         public static void main(String args[]){
              LError le=new LError();
              System.out.println("Sum is: "+le.sum(20,10));
         }
   }                                                D:\>javac LError.java
                                                        D:\>java LError
                                                        Sum is: 10
                           What is an Exception?
 An Exception is an abnormal situation (or) unexpected situation in the
  normal flow of the program execution.

 An exception is an abnormal condition that arises in a code sequence at run
  time. In other words, an exception is a run-time error.

 Because of Exceptions the flow of program execution is getting
  disturbed so that program execution may continue (or) may not be
  continued.
    Examples
        •   Division by zero
        •   Attempts to use an array index out of bounds.
        •   Number input in wrong format
        •   Unable to write output to file
        •   Missing input file
                   Exception Handling
   Whenever an exception is occurred, handling those exceptions
    called as “Exception Handling”.

   Performing action in response to exception

    Examples
    – Exit program (abort)
    – Ignore exception
    – Deal with exception and continue
        • Print error message
        • Request new data
        • Retry action
           Benefits of Exception Handling

 It allows us to fix the error.

 It prevents program from automatically terminating.

 Separates Error-Handling Code from Regular Code.
   – Conventional programming combines error detection,
      reporting, handling code with the regular code, leads
      to confusion.




                                                              9
         Exception-Handling Fundamentals

 A Java exception is an object that describes an exceptional
  condition that has occurred in a piece of code.

 When an exceptional condition arises, an object representing
  that exception is created and thrown in the method that caused
  the error.

 An exception can be caught to handle it or pass it on.

 Exceptions can be generated by the Java run-time system, or
  they can be manually generated by your code.



                                                                   10
                                                      (contd..)

 Java exception handling is managed by via five keywords:
  try, catch, throw, throws, and finally.

 Program statements to monitor are contained within a try
  block.

 If an exception occurs within the try block, it is thrown.

 Code within catch block catch the exception and handle it.

 System generated exceptions are automatically thrown by the
  Java run-time system.



                                                                  11
                                                  (contd..)
 To manually throw an exception, use the keyword throw

 Any exception that is thrown out of a method must be
  specified as such by a throws clause.

 Statements contained in the finally block will be executed,
  regardless of whether or not an exception is raised.

 An exception is an error which can be handled. It means an
  exception happens, the programmer can do something to avoid
  any harm.

 But an error is an error which cannot be handled, it happens
  and the programmer cannot do anything.
   Java Exception Class Hierarchy
                                                            …
                                                       EOFException

                                 IOException           FileNotFoundException

                     Exception
                                                           ArithmeticException


                                                           NullPointerException
                                 RuntimeException

                                                           IndexOutOfBoundsException
                                  …
Object   Throwable
                                                           NoSuchElementException



                                 VirtualMachineError
                                  Ex: Stack overflow
                     Error                                …

                                                               Checked
                                 …
                                                              Unchecked
                                 java.lang.Object
    Exception Types
                                   Throwable




                   Exception                                 Error
           Built –in Exception               User-defined Exception



  RuntimeException                Checked Exceptions
                                     Checked                Unchecked
                                  (java.lang.Exception)
                                     Exceptions             Exceptions

  Un Checked Exceptions
(java.lang.RuntimeException)
                   Exception




Built Exceptions                User Defined Exceptions

         Built –in Exception                  User-defined Exception

                     Un Checked Exceptions                Checked Exceptions
                                                               Unchecked
                                                               Exceptions
                              Exceptions Types
 Inside java.lang package, Java defines several exception classes.

 All exceptions are subclasses of Exception.

 Since java.lang is implicitly imported into all Java programs, most exceptions
  derived from RuntimeException are automatically available.

 Furthermore, they need not be included in any method’s throws list.

 These are called unchecked exceptions because the compiler does not check
  to see if a method handles or throws these exceptions.

 Exceptions defined by java.lang those must be included in a method’s throws
  list if that method can generate one of the exceptions in Table 1 and does not
  handle it itself. These are called checked exceptions.
           General Form of try-catch block

try{
    // block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
    // exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
    // exception handler for ExceptionType2
}

//Here, ExceptionType is the type of exception that has occurred.
                         Uncaught Exceptions
      class Exc0 {
           public static void main(String args[]) {
               int d = 0;
               int a = 42 / d;   java.lang.ArithmeticException: / by zero
           }                                        at Exc0.main(Exc0.java:4)
      }




                                       class Exc1 {
                                            static void subroutine() {
                                                 int d = 0;
                                                 int a = 10 / d;
java.lang.ArithmeticException: / by zero }
     at Exc1.subroutine(Exc1.java:4)        public static void main(String args[]) {
     at Exc1.main(Exc1.java:7)                   Exc1.subroutine();
                                            }
                                       }
• When the Java run-time system detects the attempt to divide by zero, it
  constructs a new exception object and then throws this exception.

• This causes the execution of Exc0 to stop, because once an exception has
  been thrown, it must be caught by an exception handler and dealt with
  immediately.

• In this example, we haven’t supplied any exception handlers of our own,
  so the exception is caught by the default handler provided by the Java
  run-time system.

• Any exception that is not caught by your program will ultimately be
  processed by the default handler.

• The default handler displays a string describing the exception, prints a
  stack trace from the point at which the exception occurred, and terminates
  the program.
                         Using try and catch
 To guard against and handle a run-time error, simply enclose the code
  that you want to monitor inside a try block.

 Immediately following the try block, include a catch clause that
  specifies the exception type that you wish to catch.

 To illustrate how easily this can be done, the following program includes
  a try block and a catch clause which processes the
  ArithmeticException generated by the division-by-zero error:
class Exc2 {
         public static void main(String args[]) {
             int d, a;
             try { // monitor a block of code.
                  d = 0;
                  a = 42 / d;
                  System.out.println("This will not be printed.");
             }
             catch (ArithmeticException e) { // catch divide-by-zero error
                  System.out.println("Division by zero.");
             }
             System.out.println("After catch statement.");
         }
}
                       Output:
                                 Division by zero.
                                 After catch statement.
• The scope of the catch clause is restricted to those statements specified by the
  immediately preceding try statement.

• A catch statement cannot catch an exception thrown by another try statement
  (except in the case of nested try statements, described shortly).

• Displaying a Description of an Exception: Throwable overrides the
  toString( ) method (defined by Object) so that it returns a string containing a
  description of the exception.

• You can display this description in a println( ) statement by simply passing the
  exception as an argument.
• For example, the catch block in the preceding program can be rewritten like
  this:

    catch (ArithmeticException e) {
        System.out.println("Exception: " + e);
        a = 0; // set a to zero and continue
    }

   When this version is substituted in the program, and the program is run, each
   divide-by-zero error displays the following message:

             Exception: java.lang.ArithmeticException: / by zero
                       Multiple catch Clauses
• When an exception is thrown, each catch statement is inspected in
  order, and the first one whose type matches that of the exception is
  executed.

• After one catch statement executes, the others are bypassed, and
  execution continues after the try/catch block.
class MultiCatch {          // Demonstrate multiple catch statements.
    public static void main(String args[]) {
      try {
           int a = args.length;
           System.out.println("a = " + a);
           int b = 42 / a;
           int c[] = { 1 };
           c[42] = 99;
      }
      catch(ArithmeticException e) {
           System.out.println("Divide by 0: " + e);
      }
      catch(ArrayIndexOutOfBoundsException e) {
           System.out.println("Array index oob: " + e);
      }
      System.out.println("After try/catch blocks.");
    }                                      C:\>java MultiCatch
}                                          a=0
                                         Divide by 0: java.lang.ArithmeticException: / by zero
                                         After try/catch blocks.
                                         C:\>java MultiCatch TestArg
                                         a=1
                                         Array index oob: java.lang.ArrayIndexOutOfBoundsException
                                         After try/catch blocks.
/* This program contains an error. A subclass must come before its super class in
a series of catch statements. If not, unreachable code will be created and a compile-
time error will result. */

class SuperSubCatch {
   public static void main(String args[]) {
     try {
          int a = 0;
          int b = 42 / a;
     }
     catch(Exception e) {
          System.out.println("Generic Exception catch.");
     }
/* This catch is never reached because ArithmeticException is a subclass of Exception. */
     catch(ArithmeticException e){                     // ERROR - unreachable
         System.out.println("This is never reached.");
     }
    }
}
                         Nested try Statements
• The try statement can be nested.

• That is, a try statement can be inside the block of another try.

• Each time a try statement is entered, the context of that exception is
  pushed on the stack.

• If an inner try statement does not have a catch handler for a particular
  exception, the stack is unwound and the next try statement’s catch
  handlers are inspected for a match.

• This continues until one of the catch statements succeeds, or until all of
  the nested try statements are exhausted.

• If no catch statement matches, then the Java run-time system will handle
  the exception.
class NestTry {           // An example of nested try statements.
    public static void main(String args[]) {
     try {
                                             C:\>java NestTry
          int a = args.length;               Divide by 0: java.lang.ArithmeticException: / by zero
          int b = 42 / a;
          System.out.println("a = " + a); C:\>java NestTry One
          try {      // nested try block     a=1
                if(a==1) a = a/(a-a);        Divide by 0: java.lang.ArithmeticException: / by zero
                if(a==2) {                   C:\>java NestTry One Two
                     int c[] = { 1 };        a=2
                     c[42] = 99;             Array index out-of-bounds:
                }                            java.lang.ArrayIndexOutOfBoundsException
          }
          catch(ArrayIndexOutOfBoundsException e) {
                System.out.println("Array index out-of-bounds: " + e);
          }
     }
     catch(ArithmeticException e) {
          System.out.println("Divide by 0: " + e);
     }
    }
}
                                        throw
 Dealt with catching an exceptions that are thrown by the Java run-time system.

 Program can also throw an exception explicitly, using the throw statement.
  The general form of throw is shown here:
                     throw ThrowableInstance;
  Here, ThrowableInstance must be an object of type Throwable or a subclass of
  Throwable.

 There are two ways you can obtain a Throwable object:
           using a parameter into a catch clause, or
           creating one with the new operator.

 The flow of execution stops immediately after the throw statement;
  any subsequent statements are not executed.

 It finds the catch which is suitable, if it does find a match, control is transferred
  to that statement.

 If not, then the next enclosing try statement is inspected, and so on.

 If no matching catch is found, then the default exception handler halts the
  program and prints the stack trace.
class ThrowDemo {            // Demonstrate throw.
   static void demoproc() {
    try {
         throw new NullPointerException();
    }
    catch(NullPointerException e) {
         System.out.println("Caught inside demoproc.");
         throw e; // rethrow the exception
    }
   }
   public static void main(String args[]) {
    try {
         demoproc();
    }
    catch(NullPointerException e) {
         System.out.println("Recaught: " + e);
    }
   }
}
                                        throws
 If a method is capable of causing an exception that it does not handle, then it
  must specify some behavior so that callers of the method can guard themselves
  against that exception.

 A throws clause lists the types of exceptions that a method might throw.

 You can use throws clause in the method’s declaration.

 This is necessary for all exceptions, except those of type Error or
  RuntimeException, or any of their subclasses.

 All other exceptions that a method can throw must be declared in the throws
  clause.

 If they are not, a compile-time error will result.
 type method-name(parameter-list) throws exception-list{
        // body of method
 }
 Here, exception-list is a comma-separated list of the exceptions that a method can throw.
// This program contains an error and will not compile.
class ThrowsDemo {
    static void throwOne() {
           System.out.println("Inside throwOne.");
           throw new IllegalAccessException("demo");
    }
    public static void main(String args[]) {
           throwOne();
    }
                     class ThrowsDemo {                 // This is correct.
}
                              static void throwOne() throws IllegalAccessException {
                                    System.out.println("Inside throwOne.");
                                    throw new IllegalAccessException("demo");
                              }
                              public static void main(String args[]) {
                                    try {
                                         throwOne();
                                    }
                                    catch (IllegalAccessException e) {
                                         System.out.println("Caught " + e);
                                    }
                              }             inside throwOne
                     }                      caught java.lang.IllegalAccessException: demo
                                     finally
 finally creates a block of code that will be executed after a try/catch block has
  completed and before the code following the try/catch block.

 The finally block will execute whether or not an exception is thrown.

 If an exception is thrown, the finally block will execute even if no catch
  statement matches the exception.

 This can be useful for closing file handles and freeing up any other resources.

 The finally clause is optional.

 However, each try statement requires at least one catch or a finally clause.

 Any time a method is about to return to the caller from inside a try/catch
  block, via an uncaught exception or an explicit return statement, the finally
  clause is also executed just before the method returns.
class FinallyDemo {                                static void procC() {
    static void procA() {                            try {
      try {                                                System.out.println("inside procC");
      System.out.println("inside procA");            }
      throw new RuntimeException("demo");            finally {
      }                                                    System.out.println("procC's finally");
      finally {                                      }
      System.out.println("procA's finally");       }
      }                                            public static void main(String args[]) {
    }                                               try {
    static void procB() {                                 procA();
      try {                                         }
      System.out.println("inside procB");           catch (Exception e) {
      return;                                       System.out.println("Exception caught");
      }                                             }
      finally {                                     procB();            inside procA
      System.out.println("procB's finally");        procC();            procA’s finally
      }                                            }                    Exception caught
    }                                                                   inside procB
                                               }
                                                                        procB’s finally
                                                                        inside procC
                                                                        procC’s finally
                         Java’s Built-in Exceptions
 Inside java.lang package, Java defines several exception classes.

 These exceptions are subclasses of the standard type RuntimeException.

 Since java.lang is implicitly imported into all Java programs, most exceptions
  derived from RuntimeException are automatically available.

 Furthermore, they need not be included in any method’s throws list.

 These are called unchecked exceptions because the compiler does not check
  to see if a method handles or throws these exceptions.

 Exceptions defined by java.lang those must be included in a method’s throws
  list if that method can generate one of these exceptions and does not handle it
  itself. These are called checked exceptions.
Java’s Unchecked RuntimeException Subclasses
Java’s Checked Exceptions Defined in java.lang
                 Table 1
          Creating Your Own Exception Subclasses
 You can create your own exception types to handle situations specific to
  your applications.

 To do this, define a subclass of Exception (which is, of course, a subclass
  of Throwable).

 The Exception class does not define any methods of its own. It does, of
  course, inherit those methods provided by Throwable.

 Thus, all exceptions, including those that you create, have the methods
  defined by Throwable available to them.

 You may also wish to override one or more of these methods in exception
  classes that you create.
 Every user defined Exception last word must be Exception since every
  exception is a class.
  E.g.,    AgeException
           MarksOutOfBoundsException

 Whenever a superclass is Exception then that type of User defined
  Exception is Checked Exception.
  E.g.,     class XYZException extends Exception{
                      // body of the class
             }
 If the superclass is RuntimeException then that user defined Exception is
  unchecked Exception.
  E.g.,     class XYZException extends RunTimeException{
                      //body of the class
            }
class MyException extends Exception { // This program creates a custom exception type.
    private int detail;
    MyException(int a) {
      detail = a;
    }
    public String toString() {
      return "MyException[" + detail + "]";
    }
}
class ExceptionDemo {
    static void compute(int a) throws MyException {
            System.out.println("Called compute(" + a + ")");
            if(a > 10)
                  throw new MyException(a);
                  System.out.println("Normal exit");
    }
      public static void main(String args[]) {
      try {
            compute(1);
            compute(20);                                     Called compute(1)
      }                                                      Normal exit
      catch (MyException e) {                                Called compute(20)
            System.out.println("Caught " + e);               Caught MyException[20]
      }
    }
}
The Methods Defined by Throwable
                             Single Tasking
 A task means doing some calculation, processing, etc.

 Generally, a task involves execution of a group of statements, for
  example executing a program.

 In single tasking environment, only one task is given to processor at a
  time.

 This means we are wasting a lot of processor time and processor has to
  sit idle without any job for a long time.

 This is the drawback in single tasking.
                               Multi Tasking
 In multi tasking environment, several tasks are given to processor at a
  time.

 Multi tasking can be done by scheduling algorithms.

 In this, most of the processor time is getting engaged and it is not sitting
  idle.


   Uses:

     To use the processor time in a better way.
     To achieve good performance.
     To make processor not to sit idle for long time.
                     Concepts of Multithreading

 A multithreaded program contains two or more parts that can run
  concurrently.

 Each part of such a program is called a thread, and each thread defines a
  separate path of execution.

 Thus, multithreading is a specialized form of multitasking.

 There are two distinct types of multitasking:
                Process-based and
                Thread-based.
 Process-based

   o A process is a program that is executing.

   o Process-based multitasking is the feature that allows your computer
     to run two or more programs concurrently.

   o In process-based multitasking, a program is the smallest unit of code
     that can be dispatched by the scheduler.

   o For example, process-based multitasking enables you to run the Java
     compiler at the same time that you are using a text editor.

   o Process-based multitasking deals with the “big picture”.
 Thread-based.
   A thread is a separate path of execution.

   o In a thread-based multitasking environment, the thread is the
     smallest unit of dispatchable code.

   o This means that a single program can perform two or more tasks
     simultaneously.

   o For instance, a text editor can format text at the same time that it is
     printing, as long as these two actions are being performed by two
     separate threads.

   o Thread based multitasking handles the details.

   o Whenever thread is created it will not occupy any separate memory.
     But it will share the memory which is already allocated to a program.
                  Process-based vs. Thread-based

1. Processes are heavy weight tasks     1.    Threads are light weight
   that require their own separate           process. They share the same
   address spaces.                           address space and cooperatively
                                             share the same heavy weight
                                             process.

                                        2. Inter thread communication is
2. Interprocess communication is
                                           inexpensive.
   expensive and limited.

                                        3. Context switching from one
3. Context switching from one
                                           thread to the next is low cost.
   process to another is also costly.
                            Thread Life Cycle
Thread can exist in several states:

New State:
 After the creations of Thread instance the thread is in this state but
  before the start() method invocation. At this point, the thread is
  considered not alive.

Ready (Runnable) State:
 A thread start its life from Runnable state.

 A thread first enters runnable state after the invoking of start() method
  but a thread can return to this state after either running, waiting,
  sleeping or coming back from blocked state also.

 On this state a thread is waiting for a turn on the processor.
                     Thread Life Cycle (Contd..)
Running State:
 A thread is in running state that means the thread is currently executing.

 There are several ways to enter in Runnable state but there is only one
  way to enter in Running state: the scheduler select a thread from
  runnable pool.

Blocked State:
 A thread can enter in this state because of waiting the resources that are
   hold by another thread.

Dead State:
 A thread can be considered dead when its run() method completes.
 If any thread comes on this state that means it cannot ever run again.
Schedular dispatch
                       Thread Life Cycle (Contd..)
 Starting from the birth of a thread, till its death, a thread exists in different
  states which are collectively called “Thread Life Cycle”.

 A thread will be born when it is created using Thread class as:
                Thread obj=new Thread();

 A thread goes into runnable state when start() method is applied on it.

 That is void start() method is used to call the public void run() method.

 From runnable state, a thread may get into not-runnable state, when sleep()
  or wait() or suspend() methods act on it.

 notify() and resume() methods are used to restart waiting and suspended
  threads respectively.
                  Thread Life Cycle (Contd..)

 yield() method causes the currently executing thread object to
  temporarily pause and allow other threads to execute.

 ThreadDeath class will be invoked whenever the stop() method is
  called.
       Single Thread System Vs. Multi Thread System

• Single-thread System use an        • The advantage of multi threading is
  approach called an event loop        that the main loop, pooling
  with pooling.                        mechanism is eliminated.

• A Single thread of control runs    • Multithreading enables us to write
  in an infinite loop, pooling a       very efficient programs that make
  single event queue to decide         maximum use of CPU. Because
  what to do next.                     ideal time can be kept to a
                                       minimum.
• In      a       single-threaded
  environment, when a thread         • One thread can pause with out
  blocks because it is waiting for     stopping other parts of the
  some resources the entire            program. Java supports multi
  program stops running.               threading.
                     Thread Class Constructors
1. Thread()              Allocates a new Thread Object.
                         E.g., Thread t=new Thread();

2. Thread(String name)       Allocates a new thread.
                         E.g., Thread t= new Thread( “FirstChild”);



3. Thread(Runnable target) Allocates a new Thread object.
                      E.g., SecondThread st= new SecondThread();
                      Thread t=new Thread(st);

4. Thread(Runnable target, String name)
                      Allocates a new Thread object.
                      E.g., SecondThread st=new SecondThread();
                      Thread t= new Thread(st, ”Secondchild”);
5. Thread(ThreadGroup group, String name) Allocates a new Thread object.

               E.g., ThreadGroup tg=new ThreadGroup(“Image Group”);
               Thread t1= new Thread(tg,”frist child”);
               Thread t2= new Thread(tg,”second child”);

6. Thread(ThreadGroup group, Runnable target)        Allocates a new thread
   object.


  Where group is the thread group, target is the object whose run method is
  called.

               E.g., ThreadGroup tg=new ThreadGroup(“Image Group”);
               SecondThread st= new SecondThread();
               Thread t1= new Thread(tg, st);
               Thread t2= new Thread(tg, st);
                         Thread Class Methods

 public final boolean isAlive()     Tests if this thread is alive.

 boolean isDaemon()                 Tests if this thread is a daemon thread.

 public final void join() throws InterruptedException
                                     Waits for this thread to die.

 public final String getName()      Returns this thread's name.

 public static void sleep(long millis) throws InterruptedException
       Causes the currently executing thread to sleep for the specified
       number of milliseconds.
                         Thread Class Methods

 public static void sleep(long millis, int nanos) throws InterruptedException
       Causes the currently executing thread to sleep for the specified
       number of milliseconds plus the specified number of nanoseconds.

 public static void yield()
       Causes the currently executing thread object to temporarily pause
       and allow other threads to execute.

 public static Thread currentThread()
       Returns a reference to the currently executing thread object.

 public final void setName(String name)
       Changes the name of this thread to be equal to the argument name.
                         Thread Class Methods

 public final int getPriority()           Returns this thread's priority.
 public final void setPriority(int newPriority)
                                        Changes the priority of this thread.
   NOTE 1: The value of newPriority must be within the range MIN_PRIORITY
   and MAX_PRIORITY (1 and 10), for default priority, specify
   NORM_PRIORITY, which is currently 5. These priorities are defined as final
   variables within Thread.

NOTE 2:A thread’s priority is used to decide when to switch from one running
 thread to the next. This is called a context switch.
       A thread can voluntarily relinquish control.
       A thread can be preempted by a higher-priority thread.

 public final ThreadGroup getThreadGroup()
            Returns the thread group to which this thread belongs. This method
            returns null if this thread has died.
                              main Thread
 In every java program, there is always a thread running internally. This
  thread is used by JVM to execute the program statements.

 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:

   o It is the thread from which other “child” threads will be spawned.

   o Often it must be the last thread to finish execution because it performs
     various shutdown actions.
class CurrentThreadDemo{
     public static void main(String sree[]){
         System.out.println(“Let us find the current thread…..”);
         Thread t = Thread.currentThread();
         System.out.println(“Current Thread= ”+t);
         System.out.println(“It’s name= ”+t.getName());
     }
}


               Let us find the current thread…..
               Current Thread= Thread[main,5,main]
               It’s name= main

Which thread always runs in a java program by default?
                   main thread
                 Creating a Thread and Running it
 Create a class that extends Thread class or implements Runnable interface.
  Both are found in java.lang package.

   class Myclass extends Thread{            class Myclass implements Runnable{
         //methods                   (or)           //methods
         //statements;                              //statements;
   }                                        }

 Write a run() method in a class.

 Create an object to Myclass.          Myclass obj=new Myclass();

 Create a thread and attach the thread to the object obj.
                                        Thread t = new Thread(obj);
 Run the thread using t.start() method.
class One extends Thread{         //Single thread example using
   public void run(){            // “extends Thread”
        for(int i=0;i<5;i++){
             System.out.println("i value: "+i);
        }
   }
}
class ThreadDemo{
   public static void main(String sree[]){
        One o=new One();
        Thread t1=new Thread(o);                       i value: 0
        t1.start();                                    i value: 1
   }                                                   i value: 2
                                                       i value: 3
}
                                                       i value: 4
class One implements Runnable{           //Single thread example using
   public void run(){                    //“implements Runnable”
        for(int i=0;i<5;i++){
            System.out.println("i value: "+i);
        }
   }
}
class RunnableDemo{
   public static void main(String sree[]){
        One o=new One();
        Thread t1=new Thread(o);                           i value: 0
        t1.start();                                        i value: 1
                                                           i value: 2
   }
                                                           i value: 3
}                                                          i value: 4
               //Multi thread example using “extends Thread”
class One extends Thread{                    class ThreadDemo{
   public void run(){                            public static void main(String sree[]){
     for(int i=0;i<5;i++){                        One o=new One();
       System.out.println("i value: "+i);         Thread t1=new Thread(o);
     }                                            t1.start();
   }
}                                                 Two t=new Two();
class Two extends Thread{                         Thread t2=new Thread(t);
   public void run(){                             t2.start();
     for(int j=10;j>4;j--){                      }
       System.out.println(“j value: "+j); }
     }
   }            D:\>java ThreadDemo     D:\>java ThreadDemo         D:\>java ThreadDemo
                i value: 0              i value: 4                  i value: 0
}               i value: 1              i value: 3                  i value: 1
              i value: 2                i value: 0                i value: 4
              i value: 4                i value: 1                i value: 3
              i value: 3                i value: 2                i value: 2
           //Multi thread example using “implements Runnable”
class One implements Runnable{               class RunnableDemo{
   public void run(){                           public static void main(String sree[]){
     for(int i=0;i<5;i++){                        One o=new One();
       System.out.println("i value: "+i);         Thread t1=new Thread(o);
     }                                            t1.start();
   }
}                                                 Two t=new Two();
class Two implements Runnable{                    Thread t2=new Thread(t);
   public void run(){                             t2.start();
     for(int j=10;j>4;j--){                     }
       System.out.println(“j value: "+j); }
     }
   } D:\>java RunnableDemo          D:\>java RunnableDemo        D:\>java RunnableDemo
        i value: 0                  i value: 4                   i value: 0
}       i value: 1                  i value: 3                   i value: 1
       i value: 2                   i value: 0                 i value: 4
       i value: 4                   i value: 1                 i value: 3
       i value: 3                   i value: 2                 i value: 2
  Difference between “extends Thread” and “implements Runnable” :

 Both are functionally same.

 But when we write extends Thread, there is no scope to extend another
  class, as multiple inheritance is not supported in java.

  class MyClass extends Thread, AnotherClass                 //invalid

 If we write implements Runnable, then still there is scope to extend
  another class.

  class MyClass extends AnotherClass implements Runnable     //valid

 This is definitely advantageous when the programmer wants to use
  threads and also wants to access the features of another class.
                              Synchronization
 When two or more threads need access to a shared resource it is called as
  race condition.

 To avoid this race condition we need to use a mechanism called as
  synchronization.

 A monitor is an object that is used as a mutually exclusive lock, or mutex.

 Only one thread can own a monitor at a given time.

 When a thread enters into the monitor all other threads attempting to enter
  the locked monitor will be suspended until the first thread exits the
  monitor.
 Synchronization can be achieved in two ways:
      1) Method level synchronization
      2) Block level synchronization

 In method level synchronization the total data of method is sensitive. Where
  as in block level synchronization part of the method is sensitive.
//Without Synchronization                 class Caller extends Thread{
class Callme{                                  Callme target;
                                               String msg;
    void call(String msg){
                                               Thread t;
    System.out.print("["+msg);
                                               Caller(Callme tar,String str){
    try {                                           target=tar;
         Thread.sleep(1000);                        msg=str;
    }                                               t = new Thread(this);
    catch(InterruptedException e) {                 t.start();
                                               }
     System.out.println("Interrupted");        public void run(){
     }                                              target.call(msg);
     System.out.println("]");                  }
                                          }
    }
                                          class SyncDemo{
}                                              public static void main(String args[]){
          [One[Three[Two]                           Callme cr=new Callme();
          ]                                         Caller t1=new Caller(cr,"One");
          ]                                         Caller t2=new Caller(cr,"Two");
                                                    Caller t3=new Caller(cr,"Three");
                                               }
                                          }
//Synchronization Example                    class Caller extends Thread{
class Callme{                                     Callme target;
                                                  String msg;
  public synchronized void call(String msg){
                                                  Thread t;
   System.out.print("["+msg);                     Caller(Callme tar,String str){
   try {                                               target=tar;
        Thread.sleep(1000);                            msg=str;
   }                                                   t = new Thread(this);
   catch(InterruptedException e) {                     t.start();
                                                  }
   System.out.println("Interrupted");             public void run(){
   }                                                   target.call(msg);
   System.out.println("]");                       }
                                             }
  }
                                             class SyncDemo{
}                                                 public static void main(String args[]){
          [One]                                        Callme cr=new Callme();
          [Three]                                      Caller t1=new Caller(cr,"One");
          [Two]                                        Caller t2=new Caller(cr,"Two");
                                                       Caller t3=new Caller(cr,"Three");
                                                  }
                                             }
class BlockSyn implements Runnable{          /Example for block level synchronization
   public void run(){
    display();                                                       first
   }                                                                 second
   public void display(){                                            i value is:0
    System.out.println(Thread.currentThread().getName());            i value is:1
    synchronized(this){                                              i value is:2
    for(int i=0;i<3;i++){                                            i value is:0
         try{                                                        i value is:1
              Thread.sleep(1000);                                    i value is:2
              System.out.println("i value is:"+i);
         }
         catch(Exception e){               public static void main(String args[]){
              System.out.println(e);            BlockSyn s= new BlockSyn();
         }                                      Thread t1= new Thread(s,"first");
                                                Thread t2= new Thread(s,"second");
    }
                                                t1.start();
   }                                            t2.start();
}                                               }
                                           }

				
DOCUMENT INFO
Categories:
Tags:
Stats:
views:12
posted:10/4/2012
language:English
pages:74