Results 1 to 6 of 6

Thread: Problem with Synchronization in java

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Problem with Synchronization in java

    Hello,
    I have a question as an ordinary user of java. What is the meaning of "synchronized". Is this a synchronized method from a class can be executed only by one thread at a time? In this case what's the point 'notifyAll() in a synchronized method if one method that can be unlocked (knowing what made a "wait ()" before). Any help regarding this will be appreciated. Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    192

    Re: Problem with Synchronization in java

    Hello,
    I am new to java, but I think I have a code which will interest you. Have a look at the below code.
    Code:
    Public class Event
    {
    	private Boolean st; / / Event Status 
    	Public Event() {
    		st = Boolean.FALSE;
    	}
    	Public synchronized void set() {
    		st = Boolean.TRUE;
            / / Unlock threads awaiting this event:
    		notifyAll(); 
    	}
    	Public synchronized void reset() {
    		st = Boolean.FALSE;
    	}
    	Public synchronized void Pending() {
    		if(st ==Boolean.FALSE) {
    			try {
    				wait(); / / Blocks until a notify ()
    			}
    			catch(InterruptedException e) {};
    		}
    	}/ / End wait
    }/ / End class

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Problem with Synchronization in java

    Hello,
    The behavior of wait () and notify () is fairly well described in the javadoc. When you go into a block synchronized. The current thread is a lock that prevents other threads from entering the block (or any other block that uses the same lock). If another thread tries to retrieve the same lock will be put on hold until it is free. And when you use wait ()This lock is released by the current thread before going to sleep so other threads can possibly use the same lock. Hope this information will help you.

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Problem with Synchronization in java

    Hello,
    The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor
    When the thread is awakened by a notify () or notifyAll ()He must take the lock again before continuing. So the threads will wake up really one after the other according to the release of the lock.
    Code:
    to wake up either through a call to the notify method or the notifyAll method.
    The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Problem with Synchronization in java

    Hello,
    The lock is the reference used in the synchronized block
    Code:
    synchronized (lock) {
          ...
    }
    Knowing that for when synchronized is used on a method, the lock is used to reference the current object (this) Or body Class representative subject for the methods static
    For example, have a look at the following part of code
    Code:
    Public class MyClass {
    	Public synchronized void method() {
    		// ...
    	}
    	
    	Public static synchronized void staticMethod() {
    		// ...
    	}
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Problem with Synchronization in java

    Hello,
    I think the above part of code which is explained is equivalent to.
    Code:
    Public class MyClass {
    	Public void method() {
    		synchronized(this) {
    			// ...
    		}
    	}
    	
    	Public static void staticMethod() {
    		synchronized(MyClass.class) {
    			// ...
    		}
    	}
    }
    If we wait, we have no right to have the lock, if you are awakened by notify or notifyAll, we have the right to lock, but we will wait our turn (in the case notify it'll be our turn in notifyAll the case we will wait our turn, we can say random).

Similar Threads

  1. Synchronization problem in external DAC
    By Tibalt in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-03-2011, 08:50 AM
  2. RSS feed synchronization problem
    By Algernon in forum Windows Software
    Replies: 7
    Last Post: 19-10-2010, 12:50 AM
  3. Synchronization problem in my iPhone after IOS 4.1 update
    By Tiger Memon in forum Portable Devices
    Replies: 4
    Last Post: 15-10-2010, 12:20 PM
  4. Synchronization Problem with china E75 mobile
    By Balamani in forum Portable Devices
    Replies: 3
    Last Post: 08-12-2009, 03:01 AM
  5. What is synchronization in Java And why it is important?
    By Adalbert in forum Software Development
    Replies: 2
    Last Post: 20-10-2008, 02:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,631,409.13451 seconds with 16 queries