Results 1 to 2 of 2
  1. #1
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

    Default Some query about synchronized

    Java Code:
    public class SyncTest {
    	
        public static void main (String[] args) {
    
        MysyncTest runna = new MysyncTest();
        Thread t1 = new Thread(runna);
        Thread t2 = new Thread(runna);	
        
        t1.start();
        t2.start();
        
        }	
    }
    
    
    class MysyncTest implements Runnable{
    	int data = 0 ;
    	int count = 1 ;
    	
    	synchronized public int getData()
    	{
    	    data  = data + count ;
    	    try {
    			Thread.currentThread().sleep(5000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return data;
    	}
    	
    	public void setData(int a )
    	{
    		data = a ;
    	}
    
    	
    	public void syncBlock()
    	{
    		synchronized (this) {
    			// do something
    			
    		}
    	}
    	
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
            	    ++count;
    		if(count%2 == 0 )
    		{
    			getData();
    		}
    		else
    		{
    			setData(count);
    		}
    	
    	}
    }


    Query 1 : Very first time , t1 control will enter in getData() since it is a sync function ,it will acquire lock (object lock ) and this will go to sleep ... Now t2 gets cycle to execute and it will enter in to setData() .. I wanna know since t1 has the lock for the same object how t2 is using that object ??


    Query 2 : what is the diff between synchronised function and synchorized(this)

    Java Code:
    	synchronized public void fn1(){
    		
    	}
    	
    	public void fn2(){
    		synchronized (this) {
                     // Do something here 			
    		}
    	}

    Query 3 : I understood static synchronized , but I dont the know where it can be used .. can anyone help me out ?

  2. #2
    Gobi is offline Member
    Join Date
    Jun 2011
    Posts
    13
    Rep Power
    0

    Default Re: Some query about synchronized

    In addition to the above query , I have one more ..
    when you say synchronized(this) -> a additional lock byte allocated in the heap along with the object
    what abt static synchronized fn1() -> how the lock mechanism works , where the lock byte is ?

Similar Threads

  1. synchronized(this){}
    By zserzs in forum Threads and Synchronization
    Replies: 6
    Last Post: 03-21-2012, 03:08 AM
  2. synchronized statements query?
    By castiel in forum New To Java
    Replies: 1
    Last Post: 02-11-2011, 06:26 PM
  3. Replies: 1
    Last Post: 09-08-2010, 05:08 PM
  4. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 02:20 PM
  5. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 11:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •