synchronized Java Keyword

The synchronized keyword may be applied to a method or statement block and provides protection for critical sections that should only be executed by one thread at a time.

Examples

  public class MyClass
  {
     public synchronized static String mySyncStaticMethod()
     {
     }
	 
     public synchronized String mySyncMethod()
     {
     }
  {
  
  public class MyOtherClass
  {
     Object someObj;
	 
     public String myMethod()
     {
        <statements>
        synchronized (someObj)
        {
           <statements affecting someObj>
        }
     }
  }
  

Remarks

Related Topics

None.