Performance of ReentrantLock and Synchronized

19 Apr 2011 by

ReentrantLock and Synchronized

ReentrantLock and Synchronized

Synchronized keyword is famous in locking shared variables to be modified by only one thread at the specific time. However, it has performance issue while multiple threads are trying to contend for accessing shared variables. JDK 5.0 is redesigned memory model that makes it truly cross platform for multithread environment. JDK 5.0 adds java.util.concurrent package that supports various optimized concurrency tools.

In this article, I present the performance of ReentrantLock and Synchronized. Actually, the performance has been reveal in Java theory and practice: More flexible, scalable locking in JDK 5.0. However, that article did not show the source code of how to do it.

Prerequisite

Continue Reading »

5 responses so far

Monitor: Mutual Exclusion in Single System

01 Apr 2011 by

Monitor

Monitor

Monitor and Semaphore is somehow the same. However, monitor adds more abstract layer to semaphore. Semaphore refers to operation level while monitor refers to class level which is the combination of many operations and variables.

Monitor

By definition, a monitor is a collection of procedures and data that are all grouped together in a class. Just as semaphore, monitor ensures that a particular thread can exclusively access to a shared resource at a specific time. Monitor may implement semaphore and condition variable over the critical section to provide the best total single flow of modifying the resources.

Continue Reading »

One response so far

Semaphore: Mutual Exclusion in Single System

29 Mar 2011 by

Semaphore

Semaphore

Mutual Exclusion is required both in single system and distributed systems to protecting the shared resource from being modified concurrently. In this article, we will discuss the mutual exclusion in single system by using Semaphore. Finally we will follow by giving Java example. Before reading this article, it is highly recommended you read the Principle of Mutual Exclusion.

Semaphore

Semaphore is a flag that railroad engineers would use when trains enter a shared track. Semaphore here refers to a mechanism to ensure only one thread or process to access a shared resource. To implement semaphore in Java, synchronized(this) keyword should be used to surround critical section. The code from previous article will look like this.

Global variables

int x = 6;
int y = 0;

FooThread

void foo(){
  synchronized(this){
    x++;
    y = x;
  }
}

BarThread

void bar(){
  synchronized(this){
    y++;
    x += 3;
  }
}

Continue Reading »

2 responses so far

Principle of Mutual Exclusion

28 Mar 2011 by

Mutual Exclusion is a process that prevents multiple threads or processes from accessing shared resources at the same time. The problem is coming from the fact that in both distributed systems and single system, several threads or processes share a resource but cannot use it concurrently. Mutual Exclusion, Synchronization and Data Coordination are commonly use interchangeably; or just MUTEX for short.

Rationale

This will guide you to the root of the problem and give you solution.

Let see the two blocks of code that runs in parallel called foo() and bar().

The initial state: x=6 and y=0.

FooThread

void foo()
{
   x++;
   y = x;
}

BarThread

void bar()
{
   y++;
   x += 3;
}

Continue Reading »

2 responses so far

Thread Pool

18 Mar 2011 by

Thread Pool

Thread Pool

In previous post you may understand the architecture of single thread and thread per request in distributed systems. Here we will see another architecture called Thread Pool. Thread pool pre-defines a number of thread running in its pool. Let’s say we have 2 threads in the pool, then the server is capable of running only 2 requests at the same time. Other requests must wait in queue until the 2 threads in the pool finish their work. In real world applications, we see a lot of servers fall into this category. For example, Apache web server, IIS server, etc. The advantages and disadvantages are discussed below:

Continue Reading »

No responses yet

Thread per Request

17 Mar 2011 by

Thread per Request/Connection

Thread per Request/Connection

Thread per request or connection has been introduced in order to maximize the system resource and provide responsiveness to users which single thread is not capable. Thread per request or connection architecture will create a thread for each request from a client. In other words, server can create a number of threads as per request. The only limitation is software and hardware capacity. Thread per request is one among thread in distributed systems architecture. This architecture is coming with advantages and disadvantages.

Continue Reading »

No responses yet

TCP Object Transmission in Java

07 Mar 2011 by

Marshaling or Serialization

Marshaling or Serialization

A special feature in UDP is multicast. In TCP, object transmission is its special feature. This feature enables programmers to create an instantiated object in the server and then marshalling before it is actually transmitted to client for execution.

The processes of object transmission are:

  • Marshalling object to be transmitted
  • Send marshalled object over the network
  • Unmarshalling object and the execute

What is marshalling?

Marshalling and Serialization can be used interchangeably. It is the process of squashing object into a standard format in order to store in a file or be transmitted over the network.

Continue Reading »

4 responses so far

Performance Evaluation on Java Thread and .NET Thread

01 Mar 2011 by

Performance evaluation on Java single thread and multi-thread has already been discussed in the previous post. So I will show you all the performance of .NET thread comparable to Java thread. We did the same scenario and experimental environment as my last post. The overall Java performs much better than .NET both single thread and multi-thread.

Continue Reading »

No responses yet

Performance Evaluation on Single Thread and Multi-Thread

27 Feb 2011 by

Threading could eventually improve system capacity as multi-core and multi-processor are more common in commodity computer. We can spend less than USD 1000 to get Core-i3, Core-i5 or even Core-i7. So threading technique for enabling-threading tasks could maximize the overall system performance. Be remember that not all tasks could take advantage of multi-threading. And multi-thread is not always good since its overhead and cost in coordinating data. I will discuss this scenario in the later post.

Continue Reading »

One response so far

How to Return a Value from Thread

26 Feb 2011 by

Thread implementation in Java is never easier if you do following my 3 steps strictly. However, there might be a lot of question regarding return value from a thread since run() method returns void. So I will show you how to get return value from a thread.
Just to review how to implement java thread, the 3 steps are:

  1. Extends Thread class or Implements Runnable interface
  2. Override run() method
  3. In main thread, initialize thread class and then call start() method

Continue Reading »

5 responses so far