Monday, March 2, 2015

enum in Java

enum in java is a datatype which hold constant values. Please follow the below example to explore more about enum.

 
package Enumpackage;

public class enumexampleagain {
 enum days{mon(10),tue,wed(20),thu,fri(30),sat,sun(100);
 private int result;
 days(){
  System.out.println("default constructor of days");
 }
 days(int i){
  this.result=i;
  System.out.println("This day as some value "+this.result);
 }
}
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  for(days d:days.values()){
   System.out.println(d);
  }
  if(days.mon==days.tue){
   System.out.println("Do something different");
  }
  else{
   System.out.println("everyday is a new day");
  }
  
 }

}

output: This day as some value 10
default constructor of days
This day as some value 20
default constructor of days
This day as some value 30
default constructor of days
This day as some value 100
mon
tue
wed
thu
fri
sat
sun
everyday is a new day

>>>Final
------------------------------------------------------------------------------------------------------------

Daemon Thread

A Daemon Thread is a thread that runs in the background, JVM stops if all the user threads are stopped and doesn't bother about the daemon thread. Used for garbage collection, clock handler

 
package Threadsdeamon;

public class mainclass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Thread t=new Thread1();
  Runnable t2=new Thread2();
  t.setDaemon(true);
  t.start();
  new Thread(t2).start(); 
  
  if(t.isDaemon()){
   System.out.println("Daemon thread is still alive");
  }
 }

}


>>>
------------------------------------------------------------------------------------------------------------

Priority of Threads

By Default the priority of the thread is 5 and you can set the priority of the thread to minimum which is 1 and max which is 10. Below example illustrates the same.

package Threadspriority;

public class mainclass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Thread t=new thread1();
  Runnable r=new thread2();
  
  t.start();  
  new Thread(r).start();
  System.out.println(t.getName());
  System.out.println("Priority of the thread by default"+t.getPriority());
  t.setPriority(t.MIN_PRIORITY);
  System.out.println("Priority of the thread after setting "+t.getPriority());
  t.setPriority(t.MAX_PRIORITY);
  System.out.println("Priority of the thread after setting "+t.getPriority());
 }

}

>>>
------------------------------------------------------------------------------------------------------------

Synchronized Thread

By making a the run method of the thread synchronized then the corresponding thread is locked and it executes the other all threads and then execute this one. By using notify it release the threads.

 
thread1.java

package Threadsexample;

public class thread1 extends Thread {
 
 synchronized public void run(){
  System.out.println("thread one");
  
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  notify();
 }
 
}

thread2,java

package Threadsexample;

public class thread2 implements Runnable{

 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Thread 2");
  
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

mainclass.java

package Threadsexample;

public class mainclass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Thread t=new thread1();
  Runnable r=new thread2();
  
  t.start();  
  new Thread(r).start();
  System.out.println(t.getName());
 }

}


>>>
------------------------------------------------------------------------------------------------------------

Join in Threads

About join, when I use join method on a thread then it waits for that particular thread to complete/die and then proceed with the other threads.Below example explains the same.

thread1.java

package Threadsexample;

public class thread1 extends Thread {
 
 public void run(){
  System.out.println("thread one");
  
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

thread2.java

package Threadsexample;

public class thread2 implements Runnable{

 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("Thread 2");
  
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

mainclass.java

 
package Threadsexample;

public class mainclass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Thread t=new thread1();
  Runnable r=new thread2();
  
  t.start();
  try {
   t.join();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  new Thread(r).start();
  System.out.println(t.getName());
 }

}


output :
thread one
Thread-0
Thread 2
>>>
------------------------------------------------------------------------------------------------------------