Archive for the 'Java' category

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

Java RMI Database Application

20 Mar 2011 by

Java RMI Database Application

Java RMI Database Application

Java RMI has been introduced to reduce the complexity in developing protocol that relies on UDP and TCP. Java RMI provides a simpler mechanism to invoke method remote. Here I will discuss the development of Java RMI with database application. This sample application is layered into 3 tier: client, RMI server or middleware, and database.

Client is very thin and lightweight. Here client is only responsible for user interface while large business processes are handed over to RMI middleware. The middleware contains server objects distributed in registry service. The client will lookup the entire object from the registry service and invoke methods remotely. Database server stands on another layer that will be accessed by the data accessors in RMI middleware.

If you do not understand what Java RMI is, better to have a look on these articles:

Continue Reading »

21 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

Stub and Skeleton

11 Mar 2011 by

Stub and Skeleton

Stub and Skeleton

Java RMI is powerful middleware that replaces raw request/reply method as UDP and TCP. The core technology to make it works is Stub and Skeleton function. Stub is located on the client side while Skeleton is located on server side. The role of stub and skeleton is to do marshalling and unmarshalling meaning that it will squash the argument and return value of a method to be a standard format for storing in a file or transferring over the network. Without Stub and Skeleton, java RMI cannot work.

Note on Stub and Skel files (Skeleton)

  • Java JDK 1.6 does not require to have Stub and Skeleton file (Skel for short) as it takes advantage of reflection to accomplish the task
  • Java JDK 1.5, it requires only Stub file at the client. To generate stub file:
    • rmic PowerServer
    • PowerServer_Stub.class will be generated
  • Java JDK prior to 1.4 (not quite sure), it requires both Stub and Skel files
    • rmic -vcompat PowerServer
    • PowerServer_Stub.class and PowerServer_Skel.class will be generated

2 responses so far

How to Run Java RMI?

11 Mar 2011 by

There is a tricky in getting Java RMI works and runs. First, you need to understand the Overview of Java RMI and know how to implement it. Java RMI relies on 3 components–client, server and registry service.

Note: This article refers to the code developed in How to Implement Java RMI.

Registry Service

There are 2 ways to deal with registry service.

  • Run rmiregistry command in Dos prompt or Console if it is not created in the code
  • Create RMI registry service in the code

Registry registry = LocateRegistry.createRegistry(1099);

    Server

    • It needs interface class(IPower.java) created in Step 1 of How to Implement Java RMI article
    • Run java PowerServer

    Client

    • It also needs interface class (IPower.java)
    • Be sure that you pointed to the right server’s registry service
    • Run java PowerClient

    No responses yet

    How to Implement Java RMI?

    10 Mar 2011 by

    Java RMI Implementation

    Java RMI Implementation

    Implement Java RMI could be ever easier if you follow my rule strictly. You need to understand Java RMI Overview before you can catch the code.

    According to the above figure, there are 4 steps need to be involved in Java RMI development. I will give you an example of creating Java RMI application dealing with Fibonacci and Factorial calculation using badly recursive algorithm. These two methods will be available for invocation at the server side. I will call the server program as PowerServer and client program as PowerClient.

    Continue Reading »

    2 responses so far

    Marshalling and Serialization

    08 Mar 2011 by

    Marshal object to a file

    Marshal object to a file

    In practice, the terms marshalling and serialization are used interchangeably to describe the process of squashing data types into a well-defined intermediate format that is suitable for transmission on a network or for storage in a file. You might be familiar with Remote Procedure Call (RPC) as the core stub function needs to marshal the argument and return value of a method before they are actually transmitted over the network.

    This article, I will show you with an example of marshalling and unmarshalling a Java object. Be reminded that:

    • Not all data types can be serialized
    • User-defined class must implement Serializable interface
    • Attempt to marshal or serialize a non-serializable object will throw an Exception in Java

    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

    Multicast Programming with Java

    06 Mar 2011 by

    Multicast Overview

    Multicast Overview

    Multicast is a special feature of UDP protocol that enable programmer to send message to a group of receivers on a specific multicast IP address and port. Multicast has advantage in this scenario.

    Let us say I want to send “Hello” message to 100 computers on my network. Perhaps, my first solution is to send the “Hello” message to each of them via UDP or TCP.

    What a problem is this scenario?

    There are 3 generic problems:

    • Consume a lot of processing power on sender as it needs to send to every receiver
    • Bandwidth flooding
    • The arrival time is not the same for every receiver

    Continue Reading »

    10 responses so far