Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

 

and many more ....

 

 

Synchronization with multiple locks

The basic idea is to use separate locks to guard multiple independent state variables of a class, instead of having only one lock in class scope. public class Grocery { private final ArrayList fruits = new ArrayList(); private final ArrayList vegetables = new ArrayList(); public synchronized void addFruit(int index, String fruit) { fruits.add(index, fruit); } public synchronized void removeFruit(int index) ...

Read More »

Synchronized method and block

The first level of synchronization is on method scope: public class HelloSync { private Map dictionary = new HashMap(); public synchronized void boringDeveloper(String key, String value) { long startTime = (new java.util.Date()).getTime(); value = value + "_"+startTime; dictionary.put(key, value); System.out.println("I did this in "+ ((new java.util.Date()).getTime() - startTime)+" miliseconds"); } } However we should consider the basic rule of concurrency: ...

Read More »

Simple Do While loop Java Example

1. Introduction A do-while loop in Java is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block. The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the ...

Read More »

Sum Array of Numbers with for loop

This is an example of how to get the sum of the numbers in an array using a for loop. The for statement provides a compact way to iterate over a range of values. Getting the sum using a for loop implies that you should: Create an array of numbers, in the example int values. Create a for statement, with an ...

Read More »

Check for Palindrome Number with for loop

In this example we shall show you how to check if a palindrome number exists in an array, using a for loop. A palindrome number is a number that is equal to its reverse number. To check if a palindrome number exists in an array, using a for loop one should perform the following steps: Create an array of the numbers ...

Read More »

Generate Prime Numbers with for loop

With this example we are going to demonstrate how to generate prime numbers with a simple for loop. A prime number is a number that has no positive divisors other than 1 and itself. In short, to generate a prime number using a for loop you should: Create a for statement with an int i variable from 1 to a ...

Read More »

Simple for loop

This is an example of a simple for statement. The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the “for loop” because of the way in which it repeatedly loops until a particular condition is satisfied. Creating a simple for loop implies that you should: Create a for statement with an ...

Read More »

Calculate Fibonacci Series with for loop

In this example we shall show you how to calculate Fibonacci series using a for loop in Java. To calculate Fibonacci series with for loop one should perform the following steps: Create a long array with the numbers to be calculated. Create a for statement where the Fibonacci numbers are calculated and stored in the array. Then in another for statement ...

Read More »

Simple while loop Java Example

1. Introduction With this example we are going to demonstrate how to use a simple while loop Java statement. The while statement continually executes a block of statements while a particular condition is true. In short, to use a simple while loop you should: Create a while statement that evaluates an expression, which must return a boolean value and has ...

Read More »

Java Break Statement Example

This is an example of how to use the java break statement. 1. Java Break overview The break statement can be used to terminate a for, while, or do-while loop. Also it is used in switch statement to exit the current case.  2. Break in loop example In the example the break statement is used when checking an array’s elements, as ...

Read More »