Search This Blog

Saturday 21 April 2012

Thread Synchronization in Java and Android

Java is always fun love for me. Thread was the only thing that make feared so study it hard and now want to make sure that it will not happen with any one now. I have discussed about how to create simple Thread. But most important is to Synchronization of two thread.

For example -We have one file. And we have two thread one for reading this file and second for writing file. So in this case if we want to read everything that has to be written by write thread.Then Read thread to wait (if write is running) until Write does not complete it task.

For Thread Synchronization important thing is to note that we can synchronize only on one object.
Although i will write code in android technology but in java and Blackberry its almost same.For this i need to take two thread

Thread 1

private class Thread2 extends Thread{
        
        private SynDemo d;
        public Thread2(SynDemo sDemo) {
            d=sDemo;
        }
        @Override
        public void run() {
            super.run();
            d.printNumber("Thread2");
        }
    }                                                                           

This Thread will print number from 0 to 2000.Next thread also will doing the same.

Thread 2


private class Thread1 extends Thread{
        private SynDemo d;
        public Thread1(SynDemo sDemo) {
            d=sDemo;
        }
        @Override
        public void run() {
            super.run();
            d.printNumber("Thread1");
        }
      }

Now create class on which we have method on which we have to synchronize both Thread.

private class SynDemo {
        
        synchronized void printNumber(String string){
            for(int i=0;i<2000;i++){
                Log.i(""+string,String.valueOf(i));
            }
        }
    }
Now have some explanation ,Both Thread contain one constructor.In which they have SynDemo object to achieve Note that we can synchronize only on one object.In both constructor we will pass the same object Now look how
SynDemo d=new  SynDemo();  //Create one object and pass same object in both
                           // thread
Thread1 th=new Thread1(d);
Thread2 th2=new Thread2(d);
th.start();
th2.start();
Final code for android will be like this.Play with it
package com.hb;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SeekBarActivity extends Activity {
    /** Called when the activity is first created. */
    SeekBar seek;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SynDemo d=new  SynDemo();
        Thread1 th=new Thread1(d);
        Thread2 th2=new Thread2(d);
        
        th.start();
        th2.start();
    }
    
    private class Thread1 extends Thread{
        private SynDemo d;
        public Thread1(SynDemo sDemo) {
            d=sDemo;
        }
        @Override
        public void run() {
            super.run();
            d.printNumber("Thread1");
        }
      }
    
    private class Thread2 extends Thread{
        
        private SynDemo d;
        public Thread2(SynDemo sDemo) {
            d=sDemo;
        }
        @Override
        public void run() {
            super.run();
            d.printNumber("Thread2");
        }
    }
    private class SynDemo {
        
        synchronized void printNumber(String string){
            for(int i=0;i<2000;i++){
                Log.i(""+string,String.valueOf(i));
            }
        }
    }
    
}

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code