Difference between Multithreading and Synchronized Mulitthreading

Discussion in 'Java' started by manish174, Oct 5, 2010.

  1. manish174

    manish174 New Member

    Joined:
    Sep 17, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    New Delhi
    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:
    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:
    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.
     
  2. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    http://www.fashion-bop.com
    Even though the tip is very little but it has very important function! no bricks ,no mansions!
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  4. Omkarkarne

    Omkarkarne New Member

    Joined:
    Mar 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Nice Work manish174
    Realy Nice answer.
    Thanks for posting this
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice