Source Code : Static synchronize

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Static synchronize

Static synchronize
     
public class StaticSync extends Object {
  private static int nextSerialNum = 10001;

  public static synchronized int getNextSerialNum() {
    int sn = nextSerialNum;

    try { Thread.sleep(1000); } 
    catch ( InterruptedException x ) { }

    nextSerialNum++;
    return sn;
  }

  private static void print(String msg) {
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName + ": " + msg);
  }

  public static void main(String[] args) {
    try {
      Runnable r = new Runnable() {
          public void run() {
            print("getNextSerialNum()=" + 
                getNextSerialNum());
          }
        };
      
      Thread threadA = new Thread(r, "threadA");
      threadA.start();
  
      Thread.sleep(1500); 
  
      Thread threadB = new Thread(r, "threadB");
      threadB.start();
  
      Thread.sleep(500); 
  
      Thread threadC = new Thread(r, "threadC");
      threadC.start();
  
      Thread.sleep(2500); 

      Thread threadD = new Thread(r, "threadD");
      threadD.start();
    } catch ( InterruptedException x ) {
      // ignore
    }
  }
}


           
         
    
    
    
    
  

Thank with us