Tuesday, March 3, 2015

KEY METHODS FOR THREAD

Object Class

wait() (final method)
notify() (final method)
notifyAll() (final method)

Thread Class

start();
sleep(); (static method)
yield() (static method)
join() (final method)
setPriority() (final method)

Runnable Interface

run()


scheduleAtFixedRate vs scheduleWithFixedDelay

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
 long initialDelay,
 long period,
 TimeUnit unit);   
   
      Creates and executes a periodic action that becomes enabled first
      after the given initial delay, and subsequently with the given
      period; that is executions will commence after initialDelay then initialDelay+period, then
      initialDelay + 2 * period, and so on. If any execution of the task
      encounters an exception, subsequent executions are suppressed.
      Otherwise, the task will only terminate via cancellation or
      termination of the executor.  If any execution of this task
      takes longer than its period, then subsequent executions
      may start late, but will not concurrently execute.
     
Argument Parameters   
      1)command : the task to execute
      2)initialDelay : the time to delay first execution
      3)period : the period between successive executions
      4)unit : the time unit of the initialDelay and period parameters
  
Return Value
      ScheduledFuture : representing pending completion of the task, and whose  get() method will 
      throw an exception upon cancellation

 Exceptions Throws
      1) RejectedExecutionException: if the task cannot be scheduled for execution
      2) NullPointerException : if command is null
      3) IllegalArgumentException : if period less than or equal to zero
 
  
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
    long initialDelay,
    long delay,
    TimeUnit unit);  
   
      Creates and executes a periodic action that becomes enabled first
      after the given initial delay, and subsequently with the
      given delay between the termination of one execution and the
      commencement of the next.  If any execution of the task
      encounters an exception, subsequent executions are suppressed.
      Otherwise, the task will only terminate via cancellation or
      termination of the executor.

Argument Parameters
      1)command : the task to execute
      2)initialDelay : the time to delay first execution
      3)delay : the delay between the termination of one execution and the commencement of the nex  
      4)unit : the time unit of the initialDelay and delay parameters
 
Return Value  
     ScheduledFuture : representing pending completion of the task, and whose  get() method will 
     throw an exception upon cancellation
 
Exceptions Throws  
      1) RejectedExecutionException : if the task cannot bescheduled for execution
      2) NullPointerException : if command is null
      3) IllegalArgumentException : if delay less than or equal to zero
     

     
   

Monday, March 2, 2015

JAVA CODE TO DELTE DIRECTORY AND ITS SUB DIRECTORY AND FILES

private static void deleteFile(File file) {
try {
if(file==null){
return;
}
if(file.exists()) {
if(file.isDirectory()){
File[] childFiles = file.listFiles();
if(childFiles!=null && childFiles.length>0){
for (File childFile : childFiles) {
deleteFile(childFile);
}

}
file.delete();
}else{
file.delete();
}
}
}catch(NullPointerException e) {
System.out.println("Problem while Deleting folder, reason : " + e.getMessage());
}catch(SecurityException e) {
System.out.println("Problem while Deleting folder, reason : " + e.getMessage());
}catch(Exception e){
System.out.println("Problem while Deleting folder, reason : " + e.getMessage());
}
}

About Thread blocking

1)
Thread calling non-static synchronized method block each other when they works on the same instance of the respective class.
when they works on different object of the class then they do not block each other.

2)
Thread calling static synchronized method block each other because they work on the class level not on the instance level. because it is 
static synchronized method it is common among all the instances of the class.

3)
Thread calling static synchronized method and non-static synchronized method do not block each other because both are different level method.
static method is the class level method which is common for all instances and non-static method work as per instance of the class.

4)
In case of the synchronized block thread block each other if they works on the same object. Thread working on
different object will not effect each other they won't block each other.

Care:
Access to non-static fields should be done from the non-static synchronized method and
access to static fields should be done from the static synchronized methods

Java Thread's join() method

Thread's join() method

join() is a non static method, it has three forms one without any argument and one join(long millis) with the long value as a parameter
the time to wait in milliseconds. and the third is with the two parameter join (long millis, int nanos) it takes one more parameter value it represents the nano seconds value. 
means if we use this method then it will wait for the total time millis+nanos seconds.

join() method cause the current running thread to wait until the thread which calles join() on get finished.

Example
class MainThread{
public static void main(String[] box){
Thread th = new Thread();
th.join();
or
th.join(2000);
}
}

Here in this example the current running thread is the main thread. it will wait to go in runnable state until the th thread get finished.
and
same way in case of the th.join(2000), the main thread will wait  2000milli before go into runnable state or we can say main thread will join the th thread for 2000milliseconds
then it will go into the runnable state without worrying about the th thread's state.


Keep Visiting
Write your comment


Java Thread's yield() method

Thread's yield() method

It is a static method. yield() method takes the currently running thread to back to the runnable state
so the other equal's priority thread will get a chance to run. but it is not guaranteed that
the same thread will not get in execution back. it can come back in the running state.

yield() method doesn't take the thread into wait/sleep/blocking state.
it just takes the current running thread into runnable state. just for giving chance to other threads in
the Runnable pool.


Keep Visiting 
Give your comment 

JAVA THREAD PRIORITY

A thread gets a default priority that is the priority of the thread of execution that creates it.

public class MainThread {
public static void main (String [] box) {
Thread th = new Thread();
}
}
the thread referenced by th will have the same priority as the main thread.

One can also set priority using the .setPriority() method.

The thread priority value must be in range 1 to 10.

Thread priority range is also depends on the JVM some JVM takes only values between 1 to 5.

JVM never changes the thread priority.

Any thread's default priority is 5.

There are three constants in the Thread class for the thread priority.
Thread.MIN_PRIORITY (1)
Thread.NORM_PRIORITY (5)
Thread.MAX_PRIORITY (10)

If user sets  1o 10 priorities for ten different threads and if the JVM doesnt support 1 to 10 priorities but 
it support 1 to 5 priority then some of the thread among the 10 will have duplicate priorities.


Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...