Sunday, March 10, 2019

Race condition in multi-threaded application

When we develop the multi-threaded application, it's critical to ensure that no race condition occurs. Otherwise, our program will produce the inconsistent result.
Let see the demo program. When we execute this program for 5 times, it will output 1000 for each run.
public class RaceCondition implements Runnable {
static Object lock = new Object();
static int sharedNumber = 0;

public static void main(String[] args) {
Thread t1 = new Thread(new RaceCondition());
Thread t2 = new Thread(new RaceCondition());
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sharedNumber);
}

public void run() {
boolean wait = true;
for (int i = 0; i < 500; i++) {
synchronized (lock) {
int buffer = sharedNumber;
buffer++;
if (Math.random() > 0.5 && wait) {
wait = false;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
sharedNumber = buffer;
}
}
}
}
Ok, let's comment "synchronized" code line and it's closing curly brace line. This time, we will have 5 different values after executing for 5 times.
synchronized block ensure that only one thread can execute on the share/same object at a time in its block. It prevent the possible race condition.

Tuesday, March 5, 2019

How to implement CRUD features in ASP.NET Web API?

We are going to discuss how we can implement RESTful service layer using ASP.NET Web API.
In this program, we will use session object to persist as in-memory datastore.
Yes, REST is stateless. By using session, we will make it stateful. Well, the only purpose of using session in this program is to use some kind of back-end data store when we do CRUD actions. We are not using this type of data store in production code. We will use industry grade databases for that.
Alright, let's start hands-on work now. Open up Visual Studio 2015 and select File => New => Project and select "ASP.NET Web Application (.NET Framework)". Click "WebAPI" checkbox and create new project.
First we will create the Model class for Employee entity.
Model class
And make sure our WebApi return data in json format.
Configure to return JSON format
Here we will implement the API controller logic
Implement the RESTful API in the controller
In real-word application, we will have back-end database with ORM tool like Entity Framework. We will use dummy data for demo.
Generate the sample data in ActionFilter
For any WebApi, security is a very important factor. For simplicity sake, I just basic authentication to show-case the usage pattern.
For authenticating the routes
Add the entry for authentication in web.config file.
  <system.webServer>    <modules>      <add name="BasicAuthHttpModule" type="WebApiSampleProject.BasicAuthHttpModule, WebApiSampleProject" />    </modules>  </system.webServer>

Image compression using C#

Sometimes, we need to compress the image files while maintaining the image quality. This can be achieved by the following C# implementation....