Every
time I start (or think of starting) a blog post, a question arises in my mind,
‘How to start? What to write in the first paragraph?’Almost every time, an idea
gets popped up in my mind, but today, my mind blanked out. I didn’t get any
idea despite of thinking for more than 15 minutes. I even fell asleep a couple
of times during that thought process. Then I thought of googling it. I opened
google and typed ‘How to start a blog post’, I opened first 2-3 links, went
through first link (patience: 100%), found nothing related to this, then went
through the second one (patience: 50%), still, same result, then went through
the third one hoping that this will really help me, but alas, nothing useful
again(patience: 0%). Then I thought, ‘screw this’ and clicked on red button on
upper right corner. And here we are, done with the first paragraph! Bingo!
Coming to the main aspect of this blog, this blog discusses about the features which are less known in java but are really useful. They help us a lot in our day to day programming. Even I was not aware of these. But when I came across such requirements, instead of implementing them with the traditional approach, I thought, ‘there must be some other way of doing it.’ Then I googled it and found the alternate solutions which are described in this blog. I have covered very small number of features in this blog. If I come across more of such features, I will definitely add them in my subsequent blog posts. We will look into these features one by one.
Double brace initialization:
Have you ever come across a requirement where you need to create a collection (say List) and fill it with the values available to you? May be you have to invoke the method which takes a collection filled with some predefined values as an argument, in this case, what will you do? You will probably go by the following traditional approach:
Coming to the main aspect of this blog, this blog discusses about the features which are less known in java but are really useful. They help us a lot in our day to day programming. Even I was not aware of these. But when I came across such requirements, instead of implementing them with the traditional approach, I thought, ‘there must be some other way of doing it.’ Then I googled it and found the alternate solutions which are described in this blog. I have covered very small number of features in this blog. If I come across more of such features, I will definitely add them in my subsequent blog posts. We will look into these features one by one.
Double brace initialization:
Have you ever come across a requirement where you need to create a collection (say List) and fill it with the values available to you? May be you have to invoke the method which takes a collection filled with some predefined values as an argument, in this case, what will you do? You will probably go by the following traditional approach:
List<String> list = new
ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
//pass this list to the method
Or
if your list is static then, you will probably initialize it like this
public static final
List<String> list = new ArrayList<String>();
static{
list.add("A");
list.add("B");
list.add("C");
}
Java provides a way to do this in a single go, by
double brace initialization. All we need to do is to pass all the calls to add
method while creating the collection itself, as described in the code below:
List<String> list = new
ArrayList<String>(){{
add("A");
add("B");
add("C");
}};
//pass
this list to the method
Voila! Your list is created
in a single go. Isn’t this wonderful? You can do the same for static list as
well. Here are some technical details of this double brace initialization.
The
first brace creates a new anonymous inner class, the second declares an
instance initializer block that is run when the anonymous inner class is
instantiated. This type of initializer block is formally called an
"instance initializer", because it is declared within the instance scope
of the class.
This
is just an excerpt; you can find the full article here.
Add shutdown hook:
Shutdown
hook contains the code which is executed when an application goes down. Suppose
if you want to do some activities before the application gets shut down (just
like destroy() method of servlet) i.e. cleaning up resources, closing
connections and streams, notify the administrator/user about the shutdown then
you can use this shutdown hook to do the same. Following code shows how to do
it.
public static void main(String[]
args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Inside
Add Shutdown Hook...");
}
});
System.out.println("Last
instruction of Program....");
System.exit(0);
}
Output
of this program will be:
Last instruction of Program....
Inside Add Shutdown Hook...
We
can attach any number of shutdown hooks to the application. But but but, all
shutdown hooks will run in parallel as they run in separate threads. Hence, in
case of multiple shutdown hooks, we need to consider all the aspects of
concurrency, deadlock, thread interaction etc.
We can also remove the shutdown hook by calling Runtime.removeShutdownHook(Thread hook)method.
The case when these shutdown hooks will not be executed is when JVM crashes unexpectedly or is killed by the user. Because, after all, it’s JVM which executes all these instructions. Hence, no JVM means no execution. More on shutdown hooks is described here and here.
Atomic classes (part of Java.util.concurrent.atomic package):
Atomic classes (e.g. Atomic Integer, Atomic Long etc) can be very useful when you want to share the variables across multiple threads. They provide “lock free thread safe programming on single variables” i.e. you can leave aside the concurrency problems if you use this classes. They have incrementAndGet() method which will provide you the next value.
Say, if you have requirement like how many threads have used a particular method or if you want to have some kind of counter which needs to be shared across the threads and these classes fit the bill. More on Atomic package is described here. A practical example of Atomic Integer is also shown here.
I am done with the features here. Hope you have liked it and will use it in future if required. Now starts the problem of what to write in the ending paragraph. My mind has again blanked out about this and I am not in a mood to google again. Hence, I am leaving it here.
A special thanks to my colleague Harshal Choksi who inspired me to write the blogs. Thanks for the inspiration mate. Keep inspiring people like this.
Happy coding!
We can also remove the shutdown hook by calling Runtime.removeShutdownHook(Thread hook)method.
The case when these shutdown hooks will not be executed is when JVM crashes unexpectedly or is killed by the user. Because, after all, it’s JVM which executes all these instructions. Hence, no JVM means no execution. More on shutdown hooks is described here and here.
Atomic classes (part of Java.util.concurrent.atomic package):
Atomic classes (e.g. Atomic Integer, Atomic Long etc) can be very useful when you want to share the variables across multiple threads. They provide “lock free thread safe programming on single variables” i.e. you can leave aside the concurrency problems if you use this classes. They have incrementAndGet() method which will provide you the next value.
Say, if you have requirement like how many threads have used a particular method or if you want to have some kind of counter which needs to be shared across the threads and these classes fit the bill. More on Atomic package is described here. A practical example of Atomic Integer is also shown here.
I am done with the features here. Hope you have liked it and will use it in future if required. Now starts the problem of what to write in the ending paragraph. My mind has again blanked out about this and I am not in a mood to google again. Hence, I am leaving it here.
A special thanks to my colleague Harshal Choksi who inspired me to write the blogs. Thanks for the inspiration mate. Keep inspiring people like this.
Happy coding!