All three classes Executor, ExecutorService, and Executers are part of Java's Executor framework which provides thread pool facilities to Java applications. Since creation and management of Threads are expensive and operating system also imposes restrictions on how many Threads an application can spawn, it's a good idea is to use a pool of thread to execute tasks in parallel, instead of creating a new thread every time a request come in. This not only improves the response time of application but also prevent resource exhaustion errors like "java.lang.OutOfMemoryError: unable to create new native thread". A thread pool which is created when an application is a startup solves both of these problems. It has ready threads to serve clients when needed and it also has a bound on how many threads to create under load.
You might not be aware that Java 8 update 20 has introduced a new feature called "String deduplication" which can be used to save memory from duplicate String object in Java application, which can improve the performance of your Java application and prevent java.lang.OutOfMemoryError if your application makes heavy use of String. If you have profiled a Java application to check which object is taking the bulk of memory, you will often find char object at the top of the list, which is nothing but internal character array used by String object. Some of the tools and profilers might show this as java.lang.String as well e.g. Java Flight Recorder, but they are essentially pointing to the same problem i.e. a major portion of memory is occupied with String objects.
What is the difference between COALESCE and ISNULL is one of the frequently asked Microsoft SQL Server interview question. Recently, one of my reader asked the same question to me, he got confused between these two because both are used to replace NULL values to default values in SQL Server. I'll try to answer this question here with little bit of insight about when to use ISNULL and COALESCE in SQL Server. Even though both ISNULL and COALESCE is used to provide default values for NULLs there are some key differences between them e.g. ISNULL() is a T-SQL or Microsoft SQL Server specific function or operator, and datatype and length of result depends upon parameter, but COALESCE is a SQL ANSI standard, it can accept more than one parameter and unlike ISNULL, the result doesn't directly depends upon parameter, it is always the type and length of the value returned.
The common way to create objects in Java is by using public constructors. A class provides public constructor e.g. java.lang.String so anyone can create an instance of String class to use in their application, but, there is another technique which can be used to create objects in Java and every experienced Java programmer should know about it. A class can provide a public static factory method which can return an instance of the class e.g. HashMap.newInstance(). The factory method is a smart way to create objects in Java and provides several advantages over the traditional approach of creating objects using constructors in Java. It can also improve the quality of code by making the code more readable, less coupled, and improves performance by caching.
The volatile modifier has always been an interesting and tricky topic to many Java programmers. I still feel that it's one of the most underutilized modifiers in Java, which can do a lot of good if understood and applied correctly, after all, it provides a lock-free way to achieve synchronization in Java. If a field is shared between multiple threads and one of them change its value i.e. one thread reads from the field which is written by other threads, then, by using a volatile modifier, you can synchronize access to this field. The volatile modifier in Java provides visibility and ordering guarantee without any locking. You might know that compiler and JVM can re-order your code due to various reasons e.g. performance improvement which can be a problem in concurrent Java application.
Labels: Java multithreading Tutorials
The Insertion sort is another simple sorting algorithm, which can be used to sort any linear data structure like an array or linked list. On simplicity, this is next to bubble sort, and it’s also pretty close to how humans manually sort something (for example, a hand of playing cards). As the name suggest, Insertion sort is based upon insertion of an element in a sorted list. To start, we assume that first element is already sorted. Then we pick the next element and put it in second place, we compare this number with the first element and if they are not in sorted order, we swap them. This gives a new sorted list of 2 elements. Now we pick the third element and put it in the 3rd place and compare it with the 2nd placed number, if they are not in sorted order, we swap them again, if all three elements are still not in sorted order then we again swap the 1st and 2nd element, now we have a sorted list of three numbers.
Difference between Comparison (QuickSort) and Non-Comparison (Counting Sort) based Sorting Algorithms?
For many of you, this might be a surprise that how you can sort or arrange items without comparing with each other, but it's possible. There are some sorting algorithms that perform sorting without comparing the elements rather by making certain assumption about the data they are going to sort. The process is known as non-comparison sorting and algorithms are known as the non-comparison based sorting algorithms. No comparison sorting includes Counting sort which sorts using key value, Radix sort, which examines individual bits of keys, and Bucket Sort which examines bits of keys. These are also known as Liner sorting algorithms because they sort in O(n) time. They make certain assumption about data hence they don't need to go through comparison decision tree.
I have shared a lot of books related to Java and related technologies in the past e.g. Spring, Hibernate, JVM internals and performance tuning, multi-threading and concurrency, design patterns, data structure and algorithms etc, but I haven't shared core Java books for beginners yet. Even though I have mentioned a couple of popular titles every now and then, I really didn't have a good compilation of essential core Java books for beginners. Since Java is one of the most popular programming languages and has very good job prospect it has became a choice of programming language to start learning coding and application development, it makes sense to start well with core Java. This article contains books which will teach you how to code using Java programming language.
The SQL, short form of Structured Query Language is one of the essential skills in today's programming world. No matter whether you are a Java developer, C++ developer or Python developer, you must know how to write SQL queries. Every programming job interview has at least one or two questions which require you to write SQL query for given requirement and many developers struggles there. It's easy to answer theoretical questions like what is the difference between clustered and non-clustered index (see) or what is the difference between correlated and non-correlated subqueries (see), but when it comes time to actually write SQL queries to solve problems, it's not that easy, especially if you haven't done your homework and practice.
Are you preparing for Coding Interviews? If yes then you might know that there are a number of free online resources to practice important topics for coding Interviews e.g. data structure and algorithms, database and SQL, and others. These websites are equally useful for both, new programmers who are just learning the fundamentals and for experienced ones who are brushing up their coding skills for interviews. I have been sharing useful resources for programming and technical interviews from a long time in this book. In past, I have shared some recommended books for coding interviews and some of the frequently asked programming interview questions from tech companies (see here). But, my readers have been asking about some recommended websites for practicing programming challenges and coding interview questions.
How to Consume JSON from RESTful Web Service and Convert to Java Object - Spring RestTemplate Example
So far, I have not written much about REST and RESTful web service barring some interview questions e.g. REST vs SOAP, which is thankfully very much appreciated by my readers and some general suggestions about best books to learn REST in past, but today I am going to write something about RESTTemplate class from Spring MVC framework. Like its predecessors JdbcTemplate and JmsTemplate, the RestTemplate is another useful utility class which allows you to interact with RESTful web services from a Java application built using Spring framework. It's a feature rich and supports almost all REST methods e.g. GET, POST, HEAD, PUT or DELETE, though we'll only use the GET method in this article to consume a RESTful Web Service and convert the JSON response to Java objects. It's one of the basic but interesting examples, given you will often find scenarios to consume a RESTful web service from Java program.
The java.util.Calendar class was added in Java on JDK 1.4 in an attempt to fix some flaws of the java.util.Date class. It did make some task simpler, e.g. create an arbitrary date comes easier using new GregorianCalendar(2016, Calendar.JUNE, 11) constructor, as opposed to Date class where the year starts from 1900 and Month was starting from zero. It didn't solve all the problems e.g. mutability and thread-safety of Date class still remains, but it does make life easier at that time. Now with Java 8 everything related to Date and Time has become super easy and consistent but unfortunately, it will take another 5 to 10 years before older version of Java goes away. Don't believe me, there are still applications running on JDK 1.5 which was released 12 years ago. The bottom line is it's still important to know about Date and Calendar in Java.
There has been a lot of articles and books written on how to use wait and notify in Java, how they work, when do you need synchronization, and how to write concurrent code in Java, but, unfortunately, I still see many Java programmer struggles to solve even the classic producer-consumer problem. I know, writing correct concurrent code is challenging and it takes a lot of practice, think through ability and experience to get it right, but at the same time, every Java programmer should be familiar with basics of inter-thread communication, synchronization, locking and understand how all the things work together. They should be able to reason the output of program when the same code is executed by more than one thread, at the same time. They should know that compiler can re-order their code to optimize performance and how they can prevent it.
The REST has now become the standard way to develop web services. It's no more SOAP, who used to rule the world in last two decade. The SOAP and XML went very well together in past decade but it seems now REST and JSON have overtaken then and doing even better. Since Java is one of the most popular languages to develop backend services, there is a lot of demand for Java developers who knows web services and can implement and expose existing SOAP-based services into REST style, light-weight web services. This is an excellent opportunity for intermediate and experienced Java developers to learn a more job-oriented skill to keep themselves marketable and many Java developer has already started learning REST and RESTful web services in Java.
The JAX-RS is a Java specification request (JSR 311 & JSR 339) which standardize development and deployment of RESTful web services using Java and JEE technologies. It provides API in Java Programming language to create web services according to the REST (Representational State Transfer) architectural pattern. Both Restlet and Jersey are two of the most popular implementation of JAX-RS used for developing RESTful web services in Java ecosystem but there are a couple of other implementation also exist e.g. Apache Wink, Apache CXF, and JBoss RESTEasy. In this article, I'll introduce with these RESTful web services framework in Java world. It's not a very detailed post about strength and weakness of each of the framework but will just give you enough to understand them in detail later.