Saturday, December 16, 2017

How to install Maven on Windows: 4 Steps(with Screenshots)

Although Apache Maven could be installed on Windows, Linux, Solaris and Mac OS systems but today we would discuss about how to install Maven on Windows system. Don't worry if you have Unix based system because we would providing some hints, using that one could easily install Maven in their Unix based systems also.

Tuesday, December 12, 2017

Differences between @RequestParam and @PathVariable annotations in Spring MVC?

Have you came across @RequestParam & @PathVariable annotation used in the Spring MVC framework? If your answer is yes, then do you know exactly when, one should use @RequestParam or @PathVariable annotation because both the annotations are used to extract data from the incoming request. If your answer is no, then you must learn/know about these annotations because it's widely used in both the traditional web application development & RESTful Web Services development using Spring.

Table Of Content

1. Introduction
2. Scope
3. When to use which annotations?
4. Where and How of annotations?
5. Conclusion

How to add delay in your program?

Would like to add delay for a couple of seconds in your program written in Java, then Thread's sleep() method comes to our rescue. It suspends/delays the execution of the current thread for a specified period.

Choose the required method from overloaded version of sleep() method:
public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis, int nanos) throws InterruptedException
Note:
  • The sleep time is not guaranteed to be precise, because they are dependent on the facilities provided by the underlying OS.
  • Also, the sleep period can be terminated by interrupts. 
Example: Code snippet showing the use of sleep() method 
package com.anshulsblog;

import java.util.Date;

public class TestSleepMethod {

   public static void main(String[] args) throws InterruptedException {

      System.out.println("Time before calling sleep() method: " + new Date());
      Thread.sleep(5000);  //sleep 5 seconds
      System.out.println("Time after calling sleep() method: " + new Date());
   }
}

Output:
Time before calling sleep() method: Tue Dec 12 10:08:17 IST 2017
Time after calling sleep() method: Tue Dec 12 10:08:22 IST 2017
Look at the main() method, it throws InterruptedException. This is an exception thrown by sleep() method when another thread interrupts the current thread while sleep is active. Since this application has not defined another thread to cause the interrupt, it doesn't bother to catch InterruptedException. But one should catch the InterruptedException exception using try catch block.

References:
https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html