Java Questions Part10 - Programs on Threading, Exception

1.Write a Java program to create Multi-threading?

Answer:
class RunnableDemo implements Runnable {

   private Thread t;

   private String threadName;

   RunnableDemo( String name) {

      threadName = name;

      System.out.println("Creating " +  threadName );

   }

   public void run() {

      System.out.println("Running " +  threadName );

      try {

         for(int i = 4; i > 0; i--) {

            System.out.println("Thread: " + threadName + ", " + i);

            // Let the thread sleep for a while.

            Thread.sleep(50);

         }

      } catch (InterruptedException e) {

         System.out.println("Thread " +  threadName + " interrupted.");

      }

      System.out.println("Thread " +  threadName + " exiting.");

   }

   public void start () {

      System.out.println("Starting " +  threadName );

      if (t == null) {

         t = new Thread (this, threadName);

         t.start ();

      }

   }

}

public class TestThread {

   public static void main(String args[]) {

      RunnableDemo R1 = new RunnableDemo( "Thread-1");

      R1.start();

      RunnableDemo R2 = new RunnableDemo( "Thread-2");

      R2.start();

   }  

}

Output:
Creating Thread-1

Starting Thread-1

Creating Thread-2

Starting Thread-2

Running Thread-1

Thread: Thread-1, 4

Running Thread-2

Thread: Thread-2, 4

Thread: Thread-2, 3

Thread: Thread-1, 3

Thread: Thread-1, 2

Thread: Thread-2, 2

Thread: Thread-1, 1

Thread: Thread-2, 1

Thread Thread-1 exiting.

Thread Thread-2 exiting.

2. Write a Java program to demonstrate joining thread?

Answer: The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
class TestJoinMethod extends Thread{ 

 public void run(){ 

  for(int i=1;i<=5;i++){ 

   try{ 

    Thread.sleep(500); 

   }catch(Exception e){System.out.println(e);} 

  System.out.println(i); 

  } 

 } 

public static void main(String args[]){ 

 TestJoinMethod1 t1=new TestJoinMethod1(); 

 TestJoinMethod1 t2=new TestJoinMethod1(); 

 TestJoinMethod1 t3=new TestJoinMethod1(); 

 t1.start(); 

 try{ 

  t1.join(); 

 }catch(Exception e){System.out.println(e);} 

 

 t2.start(); 

 t3.start(); 

 } 

}  

Output:
1

2

3

4

5

1

1

2

2

3

3

4

4

5

5

3. Write a Java program to demonstrate Exception Handling?

Answer:
public class JavaExceptionExample{ 

  public static void main(String args[]){ 

   try{ 

      //code that may raise exception 

      int data=100/0; 

   }catch(ArithmeticException e){System.out.println(e);} 

   //rest code of the program  

   System.out.println("rest of the code..."); 

  } 

} 

Output:
Exception in thread main java.lang.ArithmeticException:/ by zero

rest of the code...

4. Write a Java program to demonstrate throwing an exception?

Answer:
public class TestThrow{ 

   static void validate(int age){ 

     if(age<18) 

      throw new ArithmeticException("not valid"); 

     else 

      System.out.println("welcome to vote"); 

   } 

   public static void main(String args[]){ 

      validate(13); 

      System.out.println("rest of the code..."); 

  } 

} 

Output:
Exception in thread main java.lang.ArithmeticException:not valid

5. Write a Java program to demonstrate the advantage of finally in Exception Handling?

Answer: Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.
class TestFinallyBlock{ 

  public static void main(String args[]){ 

  try{ 

   int data=25/5; 

   System.out.println(data); 

  } 

  catch(NullPointerException e){System.out.println(e);} 

  finally{System.out.println("finally block is always executed");} 

  System.out.println("rest of the code..."); 

  } 

} 

Output:

5

finally block is always executed

rest of the code...

No comments:

Post a Comment