Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Difference between Multithreading and Synchronized Mulitthreading (http://www.go4expert.com/articles/difference-multithreading-synchronized-t23492/)

manish174 6Oct2010 01:27

Difference between Multithreading and Synchronized Mulitthreading
 
Here, these two codes are used to shows the difference between normal MultiThreading and Synchronized Multithreading. Notice, see the difference in the starting and finishing of all the threads in both the code and try to find why every time when you run the program code, random thread starts first.

Usually a thread has three part :
  1. CPU : Its store the address of the entire path required.
  2. Data : It is the information carried by a thread during its life cycle.
  3. Code : It is the known as the property of thread.
Myth : A programmer relates "thread as process" but in real thread is not a process but "a part of process".

Thread can be created in two ways.
  1. By extending the thread class.
  2. By using / implementing the runnable interface.
The only difference between these two is "RUNNABLE " interface have only one abstract method called as run() but THREAD class has many other methods. A thread class also implements Runnable interface. Threads always throw an exception so it compulsory to use Exception handling while using thread class or Runnable interface.

Multithread



Here is a simple example of multithreading. In this example we had used "RUNNABLE" interface. Here we declared a public function named as run (). We created the single member of class MultiThread . This object member is used to create three of same type. We started the thread t1 first followed by t2 and then t3 respectively. But as when we execute the program we see that all the thread run in a random manner. This is the basic disadvantage while using the multi threading without synchronization.
Code: java

public class MultiThread implements Runnable //runnable interface is used to create multi threading.
{
    public void run()
    {
        for ( int i=0; i<10; i++ )
        {
            System.out.println("Thread started  " + Thread.currentThread().getName() + i);
            /* There is a method in thread class named as getName (). The main function of this function is to get the name of current thread */
            try
            {
                Thread.sleep( 500 );
                /* Here the thread will be paused for 500 mili second. Sleep () always throws Exception called as InterruptException, that’s why a try catch block is used to catch this exception. */
            }
            catch( Exception iex )
            {
                System.out.println( "" );
            }
        }
    }

    public static void main( String argu[] )
    {
        MultiThread m1 = new MultiThread( );
        Thread t1 = new Thread( m1 );
        Thread t2 = new Thread( m1 );
        Thread t3 = new Thread( m1 );

        t1.setName( " First " );
        t2.setName( " Second " );
        t3.setName( " Third " );

        t1.start( ); /* First thread t1 is started. */
        t2.start( ); /* Now thread t2 is started. */
        t3.start( ); /* Now Thread t3 has been started. */
    }
}

Synchronized Multithread



The code is same as the previous one. The only difference is that now we had used a synchronized method i.e. " public synchronized void run () ". With the help of this synchronized key word now our thread runs in a smooth flow, i.e at first one thread starts and finished up. Then the next thread will start and finish up and at last the thread will start and finish up its works.
Code: java

public class SMultiThread implements Runnable
{
    public synchronized void run () /* here we made run () as  synchronized method by using synchronized key word. */
    {
        for ( int i=0; i<10; i++ )
        {
            System.out.println( " Thread started  " + Thread.currentThread().getName() + i);
            try
            {
                Thread.sleep( 500 );
            }
            catch( Exception iex )
            {
                System.out.println( "" );
            }
        }
    }

    public static void main( String argu[] )
    {
        SMultiThread m1 = new SMultiThread( );
        Thread t1 = new Thread( m1 );
        Thread t2 = new Thread( m1 );
        Thread t3 = new Thread( m1 );

        t1.setName( " First " );
        t2.setName( " Second " );
        t3.setName( " Third " );

        t1.start ();
        t3.start( );
                t2.start( );
    }
}

Here in this example the thread named first starts followed by the thread third and second. They all started in random manner. To start them according to your priority you can use priority.

GO ahead. Try to build your own program based on this. Post your queries in comments below.

jstf 11Oct2010 16:49

Re: Difference between Multithreading and Synchronized Mulitthreading
 
Even though the tip is very little but it has very important function! no bricks ,no mansions!

shabbir 1Nov2010 16:27

Re: Difference between Multithreading and Synchronized Mulitthreading
 
Nominate this article for Article of the month - Oct 2010 Before November 15th 2010

Omkarkarne 15Feb2011 15:15

Re: Difference between Multithreading and Synchronized Mulitthreading
 
Nice Work manish174
Realy Nice answer.
Thanks for posting this


All times are GMT +5.5. The time now is 13:57.