Loading...

Wednesday, May 27, 2015

Top 10 Java Multithreading and Concurrency Best Practices

Writing concurrent code is hard and and testing correctness with concurrency is even harder. Though Java programming language provides lots of synchronization and concurrency support from language to API level, it's eventually comes to individual's diligent and expertise to write bug free Java concurrency code. These Java concurrency and multi-threading best practices are collection of some well known tips, which helps you to write better concurrency code in Java. Some of you, may be familiar with these tips, it's often worth to revise them in couple of years. These Java multi-threading and concurrency tips are from my own learning and usage, and also inspired by reading books like Effective Java and Java Concurrency in Practice in particular. I suggest reading Java Concurrency Practice two times to every Java developer, yes, you heard it correctly, TWO times. Concurrency is confusing and difficult to comprehend, much like Recursion to few programmers; and in one reading, you might not get all of it.

Monday, May 25, 2015

Sieve of Eratosthenes Algorithm to generate Prime numbers upto 100 in Java

There are many occasions when you need to generate all prime numbers upto a specified integer and one algorithm which is most often used to generate prime numbers is Sieve of Eratosthenes. Its an ancient greek algorithm to find all prime numbers upto a given number and named after the famous greek mathematician Eratosthenes. He was the first man to calculate the circumference of the earth and also known for working on calendars with leap years. A prime number is a whole number which is either divisible by 1 or itself e.g. 2, 3 and 5. You can see they are not divisible to any positive whole integer. In other words, prime number doesn't have a factor other than 1 or itself. You can use this algorithm to generate prime numbers from 1 to 100 or up-to any maximum value. In this tutorial, we will not only learn how Sieve of Eratosthenes algorithm works but also we will generate prime numbers using this algorithm and verify whether all number generated are actually prime or not.

Friday, May 22, 2015

Fixing Unsupported major.minor version 52.0 Error in Java

Unsupported major.minor version 52.0 comes when you are trying to run a class compiled using Java 1.8 compiler into a lower JRE version e.g. JRE 1.7 or JRE 1.6. Simplest way to fix this error is install the latest Java release i.e. Java 8 and run your program. If upgrading to Java 8 is not an option then make sure you use Java compiler's cross compilation feature and compile source code for lower Java version by using -target option of javac command. If you have multiple JRE installed make sure that the latest one comes first in PATH environment variable. You can get "java.lang.unsupportedclassversionerror unsupported major.minor version 52.0" while running a Java program explicitly using java command or running a Java Applet in browser or running a Java program from command line but compiled in Eclipse or any other build tool like Maven or ANT. Main reason of this error is that during compilation, you have used a higher version of JDK but during deployment, you have deployed into a lower version of JDK or JRE. Let's understand the cause and solution of Unsupported major.minor version 52.0 Error in little more detail.

TL:DR; upgrade to Java 8 or compile for lower JRE version using java -target 1.6 option.