Showing posts with label Java-Example. Show all posts
Showing posts with label Java-Example. Show all posts

Friday, April 18, 2014

Why wait, notify and notifyAll is defined in Object Class in Java


Here are some thoughts on why they should not be in Thread class which make sense to me :


1) Wait and notify is not just normal methods or synchronization utility, more than that they are communication mechanism between two threads in Java. And Object class is correct place to make them available for every object if this mechanism is not available via any java keyword like synchronized. Remember synchronized and wait notify are two different area and don’t confuse that they are same or related. Synchronized is to provide mutual exclusion and ensuring thread safety of Java class like race condition while wait and notify are communication mechanism between two thread.

2 )Locks are made available on per Object basis, which is another reason wait and notify is declared in Object class rather then Thread class.

3) In Java in order to enter critical section of code, Threads needs lock and they wait for lock, they don't know which threads holds lock instead they just know the lock is hold by some thread and they should wait for lock instead of knowing which thread is inside the synchronized block and asking them to release lock. this analogy fits with wait and notify being on object class rather than thread in Java.

Wednesday, July 13, 2011

How to Read File in Java

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a file line by line and print to the console.
*
*/
public class FileRead {
public static void main(String[] args) {
File file = new File("C:\\MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

//BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {

// Reads the line from the file and print it to the console.
System.out.println(dis.readLine());
}

// close all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Monday, June 21, 2010

Factorial Using Recursion - Java

   import java.io*;
     class Factorial{
      public static void main(String[] args) {
          try{
             BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter the number");
            int a= Integer.parseInt(object.readLine());
            int fact= 1;
            System.out.println("Factorial of " +a+ ":");   

            System.out.println(factorial(a));
           }
          catch (Exception e){}
         }
      int factorial(int n)
       {
             if (n == 1) {
             return n;
        }
        else {
            return n * factorial(n - 1);
        }
    }
  }

Factorial Examples - Java

import java.io*;
class Factorial{
  public static void main(String[] args) {
      try{
        BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the number");
        int a= Integer.parseInt(object.readLine());
        int fact= 1;
        System.out.println("Factorial of " +a+ ":");
        for (int i= 1; i<=a; i++){
              fact=fact*i;
        }
        System.out.println(fact);
    }
    catch (Exception e){}
  }
}