Saturday, 8 August 2015

Program to print even and odd numbers using two threads

package JavaBuzz.Threadmania;

class PrintEvenNumbersTask implements Runnable {

private Object lock;

public PrintEvenNumbersTask(Object lock) {
    this.lock = lock;
}

@Override
public void run() {
    synchronized (lock) {
        for (int i = 0; i <= 10; i += 2) {

            lock.notify();

            System.out.println("Even : "+i);

            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        lock.notify(); // not required if thread1 gains lock first
    }
}

}


class PrintOddNumbersTask implements Runnable {

private Object lock;

public PrintOddNumbersTask(Object lock) {
    this.lock = lock;
}

@Override
public void run() {
    synchronized (lock) {
        for (int i = 1; i < 10; i += 2) {

            lock.notify();

            System.out.println("Odd : "+i);

            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        lock.notify();
    }
}

}



public class EvenOddThreads {

public static void main(String[] args) {

    Object lock = new Object();

    PrintEvenNumbersTask printEvenNumberstask = new PrintEvenNumbersTask(lock);
    PrintOddNumbersTask printOddNumberstask = new PrintOddNumbersTask(lock);

    Thread thread1 = new Thread(printEvenNumberstask);
    Thread thread2 = new Thread(printOddNumberstask);

    thread2.start();
    thread1.start();

}

}

Output : 

Odd : 1
Even : 0
Odd : 3
Even : 2
Odd : 5
Even : 4
Odd : 7
Even : 6
Odd : 9
Even : 8
Even : 10

Friday, 7 August 2015

Writing your own custom exception in Java

1. Custom defined exception in Java


package exception.custom;

public class MyException extends Exception {

public MyException(String message)
{
super(message);
}

}

Analysis of the above code

"MyException" is our exception class name. It can be any name.
Mandatory thing to be done is our class extends "Exception" class to provide exception feature to you class. 
"Now we define a constructor of my class. This constructer takes a "String" argument. We call super class' constructor(super class here is "Exception class") and pass this string to it. Now Java creates a new Exception with the message passed in the input String

2. How to make use of the custom defined exception

Using Try/Catch block

package exception.custom;

public class CustomException {

/**
* @param args
* @throws MyException 
*/
public static void main(String[] args) {
try{
for (int i = 0; i <10; i++) {
System.out.println(i);

if(i==5){
throw new MyException("My Exception Occurred");
}
}
}

catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Ouput:

0
1
2
3
4
5
exception.custom.MyException: My Exception Occurred
at exception.custom.CustomException.main(CustomException.java:15)

Using Throws

package exception.custom;

public class CustomException {

/**
* @param args
* @throws MyException 
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
for (int i = 0; i <=10; i++) {
System.out.println(i);
if(i==5){
throw new MyException("My Exception Occurred");
}
}
}
}

Ouput:

0
1
2
3
4
5
Exception in thread "main" exception.custom.MyException: My Exception Occurred
at exception.custom.CustomException.main(CustomException.java:16)

Thursday, 23 July 2015

Does Java pass by reference or by value?

Java is always pass-by-value. The difficult thing to understand is that Java passes objects as references and those references are passed by value.


Everything is passed by value in Java.  Objects are not passed by reference in Java. Let’s be a little bit more specific here: objects are passed by reference – meaning that a reference/memory address is passed when an object is assigned to another – BUT (and this is what’s important) that reference is actually passed by value. The reference is passed by value because a copy of the reference value is created and passed into the other object.


Example of pass by value in Java

Suppose we have a method that is named “getvalue” and it expects an integer to be passed to it:
public void getvalue (int var)
{
  var = var + 5;
}
Note that the “var” variable has 5 added to it. Now, suppose that we have some code which calls the method getvalue:
public static void main(String [] args)
{
  int passing = 4;

  getvalue (passing);
 
  System.out.println("The value of passing is: " + passing);
}
Java will just create a copy of the “passing” variable and that copy is what gets passed to the getvalue method – and not the original variable stored in the “main” method. This is why whatever happens to “var” inside the getvalue method does not affect the “passing” variable inside the main method.

How does pass by value working here?

Java basically creates another integer variable with the value of 4, and passes that to the “getvalue” method. That is why it is called “pass by value” – because the value of 4 is what is being passed to the “getvalue” method, not a reference to the “passing” variable.