Scheduling a time delay in Java
A simple timer schedule can be used to create a time delay in your java program. You can make use of the class Timer from java.util package. Timer is capable of scheduling an initial time delay after which the task starts executing AND the time gap for the consecutive execution of the same tasks.
The syntax for the time schedule can be understood as
timer.schedule(<task>,<initialTimeGap>,<consecutiveTimeGaps>);
Below is a simple java program that makes use of timer to schedule an initial and consecutive time delays for a particular task.
package com.simpleCodeStuffs.timer;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class TimerWithInitialDelay {
public static void main(String args[]){
Timer timer = new Timer();
int initialDelay=5;
int consecutiveDelays=2;
final Calendar cal=Calendar.getInstance();
System.out.println("SimpleCodeStuffs!!! START "+cal.getTime()+"\n");
timer.schedule(new TimerTask() {
public void run() {
Calendar currentTime=Calendar.getInstance();
System.out.println("SimpleCodeStuffs!!! TASK "+currentTime.getTime());
}
}, (initialDelay*1000), consecutiveDelays * 1000);
}
}
On running this program, the output appears as :-
Notice in the output that, there is a time gap of 5 seconds from the commencement of the program to the execution of the task. After the initial execution, there is a time gap of 2 seconds between every consecutive execution of the tasks.
![]() |
|
Read More
Spring Tutorial
The Spring Framework is a lightweight framework for developing Java enterprise applications. It provides high performing, easily testable and reusable code. Spring handles the infrastructure as the underlying framework so that you can focus on your application.Spring is modular in design, thereby making creation, handling and linking of individual components so much easier. Spring implements Model View Container(MVC) design pattern.
In this tutorial, we’ll take you step by step towards mastering the spring concepts.
Here you go!!!! :)
Spring Basics
1. Spring framework architecture
Gives you an insight of the architectural framework of Spring.
This link provides any beginner, a start up at the basic concepts of Spring.
The IOC container is at the core of Spring framework. Read on to understand why this was needed and how it functions.
Basic tutorials with spring
1. Spring set up
Eclipse IDE set up and start up configurations for Spring support.
Your first attempt at creating and running spring program at its simplest.
3. JavaConfig to provide Spring metadata
The configuration metadata can also be provided using a JavaConfig file. Click on to see more.
Dependency Injection(DI)
1. Dependency Injection(DI) in spring
What exactly is Dependency Injection and why is it needed? What are the types of DI supported by Spring.
2. Basic example of Setter injection in spring
Basic example of setting values to attributes (of primitive Data Types to a class through the bean using setter injection
3. Basic example of Constructor injection in spring
Basic example of setting values to attributes (of primitive Data Types to a class through the bean using constructor injection
4. Setter injection for reference bean
How setter injection is done when an attribute of a class is another class.
5. Constructor injection for reference bean
How constructor injection is done when an attribute of a class is another class.
6. Constructor injection – ambiguities
Ambiguities that might arise while passing values to constructor parameters through injection.
Bean
What is a Bean and also what are the various scopes available in Bean.
2. Loading multiple configuration metadata files
How to load multiple configuration metadata files in spring.
Understanding the scope and life cycle of bean.
4. Bean of Singleton scope
Example of spring bean of singleton scope
5. Bean of Prototype scope
Example of spring bean of prototype scope
6. Working with Set,List,Map and Properties as attributes
Having List, set, map and properties as attributes of a bean class.
7. List FactoryBean, Set FactoryBean and Map FactoryBean
Dealing with ArrayList,LinkedList, TreeSet,HashSet,TreeMap,HashMap
8. Using life cycle events on a bean
The life cycle events of a bean can be used to set values to the attributes of a bean using the init method.
Spring Expression Language(spEL)
Gives the basics of what is spEL and what are its features.
First hands on using spEL to create and run a simple program using spEL
3. simple spEL using Annotation
Basic hands on using spEL wherein, java annotations are used instead of xml for providing the configuration metadata.
4. Dot operator on Bean reference
Dot operator can be used to reference the attribues of the bean.
5. Bean Method invocation using spEL
Dot operator can further be used to place calls to methods inside the referenced bean.
Usage of mathematical, relational, logical and ternary operators in spEL
7. Accessing Map and List attributes in spEL
Using spEL to access Map and List attributes inside a bean.
Aspect Oriented Programming(AOP)
1. Basics of Aspect Oriented Programming(AOP)
What is AOP. Why is it such an integral part of Spring. Terminologies, concepts and set up.
2. Basic example of Spring AOP Concept using BeforeAdvice
Taking up BeforeAdvice to understand the basic working of AOP concept.
3. Spring AOP BeforeAdvice, AfterReturningAdvice
Adding on AfterReturningAdvice functionality to the previously tried out BeforeAdvice, with a slight change in the scenario.
4. Spring AOP – AfterThrows Advice
Implementing AfterThrows Advice to implement a functionality once a method throws an exception.
A one stop shop for the above 3 mentioned functionalities. A combination of Before, AfterReturning and AfterThrows functionality in Around Advice
Read More
