Sunday, 28 July 2013

Advanced Multi Threading

Since java 5, concurrent package has added many features in Threading. Lets see some of the classes from this package.

1.Difference between Runnable and Callable



Multi Threading

This blog explains basic principles of Java Threading and how to create a multi-threaded application.

Threads are programs that run simultaneously. A very practical example would be the situation where a user is watching a movie and at the same time downloading something from the Internet. Here video player and Internet down-loader are 2 threads running simultaneously.

Java provides three ways to create Threads. That's right. There are three ways of creating Thread and not two.

  1. Extend Thread class
  2. Implement Runnable interface
  3. Implement Callable interface
1. Extend Thread Class

package com.hunaid.thread;

public class MyThread extends Thread{

 public void run(){
  System.out.println("MyThread run called");
  for(int i=0;i<=5;i++){

Here we have created a class which is extending Thread class. We have also implemented a method called run(). This is the method in which the logic should come which needs to run simultaneously with other Threads.

package com.hunaid.thread;

public class ThreadTest {

 public static void main(String args[]){
  MyThread myThread = new MyThread();
  System.out.println("In Main after starting MyThread");

Here we have created a instance of MyThread class and called the method start() on it.
This will bring the thread MyThread from new to runnable state. And most probably start the thread instantly. The reason I mentioned most probably is that it is up to the JVM to move the thread from runnable to running state. So if we assume that the thread will start instantly on calling start() method. The output will be as
MyThread run called
In Main after starting MyThread

The text 'In Main after starting MyThread' could be printed anywhere between 0 to 5. That is because while the thread MyThread is running JVM could decide to give the main thread that is main() method the execution slot.

In the next chapter lets see how to implement thread with Runnable interface.

Thread class >> Runnable Interface >> Callable Interface  >> CountDownLatch  >> CyclicBarrier

Web Service

Hello All,
This blog gives a basic understanding of what a Web Service is and how can a Web Service be created and tested in Java.


The figure shows the basic flow and working of a webservices. The figure shows 3 entities. Service provider, Service Requester and Service Broker. Lets first define all the entities and then we will see how they interact with each other.

Service Provider: It is a web application in a nutshell created in any technology(Java, .Net, PHP) which hosts the service on internet ( HTTP ).

Service Requester: It is a application which calls the Web Service hosted on internet. Their is no binding on which technology the client is made in.

WSDL - Web Service Description Language: As the name suggests this is a file which describes the Web Service. This is a XML file which is used by the client to create the Web Service call. We will look into this in more detail in below sections.

UDDI - Universal Description, Discovery and Integration : UDDI is a platform-independent, Extensible Markup Language (XML)-based registry by which businesses worldwide can list themselves on the Internet, and a mechanism to register and locate web service applications.

Brief Description:
A organization which wants to sell service is the one called as Service Provider. Consider a organization that provides a Web Service to send SMS to mobiles. Lets call it SMS Provider. The SMS Provider will create a web application and host it on the internet. This web application is not the usual web application that is accessed by browser. This is a web application which is configured to be called by application based on a Protocol. This protocol is called SOAP.
SOAP is a XML message format having predefined tags like Envelope, Body, Fault etc. The client application sends a SOAP request on HTTP to the service and the SMS Provider will in turn send SMS and then send a response in SOAP forma back to the client application as a response. The client application does not need to have the jars of the Web Service to call it like we need in any java application or also in EJB. The client will just have the WSDL file which represents and describes the Web Service entities like method name, method parameters, return type, endpoint, etc. From this wsdl XML file the client application can create the stub which will call the Web Service. The client application can be in any technology. It is just that it should create a XML SOAP request and submit it to the endpoint URL.

On the next page we will create a Web Service Host. Click on the link below to go to the next chapter.
Web Service Host Example

Below are some posts that explain how to implement and test SOAP/REST Webservices

Spring-ws Spring-MVC-REST
JAX-WS(wsimport) Google REST APP

Share the post