Tuesday, September 29, 2015

How to handle Facebook Likes in thread safe way in Java(Multithreading + synchronized)

12:50 AM By

Multi threading is a concept where multiple operation can be done at the same time. For example, facebook likes. Single photo or Facebook pages can be liked by many Facebook users at that same time.
Today I am going to demonstrate how to handle it in thread safe way and how it will create problem if it is not thread safe.

1. create package with name: mthread
2. Create FacebookPage called FacebookLike as shown in below.


SourceCode: FacebookLike.java

3. Create Java file called SynchronizeFbLike as shown in screenshot:


Source: SynchronizeFbLike.java

We have created Object of FacebookLike page first. final keyword is added as we can only use either final or static object/variable inside Thread.
We have created four different anonymous threads. They have override run methods and inside run we have callled plusone() method of facebook page. Same thing is done in all the threads.
Initial value of likes in now 500(set from construtor).
This program is completely thread safe as we have used synchronized keyword in plusone() method.

So this is the output:


You can see result is working as expected. Now lets see how multi user program can create problem if it is not thread safe.

> remove synchronized keyword from FacebookPage>Plusone() method.
Output is like this:

Now you can see result is totally wrong. If four user hit plusone() result should be 501,502,503,504. Now one thread have replace value incremented by another thread. If we add synchronized keyword. Java add lock in plusone() method so that one thread can only call plusone() method at once.

Hope you have understand basic about how multi threading happen in real world example scenario.
If you enjoy this article feel free to like, share and comment.

Tuesday, September 22, 2015

Console Calculator program using Java

11:44 PM By

This is sample Calculator program based on console input. You will be allowed to give input repeatedly until you type "exit". 
Is does not check for precedence of character. It start to calculate from right to left recursively.

Here is the code: 
import java.util.Scanner;

/**
 * Sample Calculator without precedence check.
 * @author ojhay
 * 
 */
public class CalculatorConsole {
public static void main( String[] args ) {

Scanner sc = new Scanner( System.in );

System.out.println( "Calculator:" );
while ( true ) {

System.out.print( "Type arithmetic Expression: " );
String expression = sc.nextLine( );

// Exit if user input: exit
if ( expression.equalsIgnoreCase( "exit" ) ) {
break;
}

double result = expre( expression );
System.out.println( expression + "= " + result );

}
sc.close( );
}

// Recursive method to evaluate arithmetic expression.
public static double expre( String expre ) {

if ( ! ( expre.contains( "+" ) || expre.contains( "-" ) || expre.contains( "*" ) || expre.contains( "/" ) ) ) {
return Integer.valueOf( expre );
}

double result = 0;
for ( int i = 0; i < expre.length( ); i++ ) {

char symbol = expre.charAt( i );
if ( !Character.isDigit( symbol ) ) { // Symbol

double operand1 = Integer.parseInt( expre.substring( 0, i ) );

switch ( symbol ) {
case '+':
result = operand1 + expre( expre.substring( i + 1 ) );
break;
case '-':
result = operand1 - expre( expre.substring( i + 1 ) );
break;
case '*':
result = operand1 * expre( expre.substring( i + 1 ) );
break;
case '/':
result = operand1 / expre( expre.substring( i + 1 ) );
break;
}

break;
}
}
return result;
}

}