You are here:

Java/doubt on synchronized block-4

Advertisement


Question
Hi sir!
the general form of synchronized block is

class table     
  {     
  .......    
   void printTable(int n)     
  {     
  synchronized(obj)     
  {    
   ......    
   }     
  }    
   }    
--------------------------------------------------------------
In the above code,i wrote synchronized(obj).Here obj means object of table class or it may be object of a string class or object of
Integer class or it may be any object of any class.
If we write this in place of obj it means it is object of table class.That means we are geting lock on table class.
In below programme I got lock on table class by putting this in place of obj.
But I am not able to write a programme to get lock on object of other than table class.
I want a programme which gets lock on object other than table class.
can you give such type of programme?
you no need to write entire programme.you simply edit below programme.
--------------------------------------------
  import java.io.*;    
      class table    
      {    
      void printTable(int n)    
      {    
      synchronized(this)    
      {    
      for(int i=1;i<=5;i++)    
      {    
      System.out.println(n*i);    
      try{    
      Thread.sleep(500);    
      }    
      catch(InterruptedException ie)    
      {System.out.println(ie);    
      }    
      }    
      }    
      }    
      }    
      class MyThread1 extends Thread    
      {    
      table t;    
      MyThread1(table t)    
      {    
      this.t=t;    
      }    
      public void run(){    
      t.printTable(5);    
      }    
      }    
      class MyThread2 extends Thread    
      {    
      table t;    
         
      MyThread2 (table t)    
      {    
      this.t=t;    
      }    
      public void run()    
      {    
      t.printTable(100);    
      }    
      }    
      class synchronizedblock1    
      {    
      public static void main(String args[]    
      )    
      {    
      table t=new table();    
      MyThread1 t1=new MyThread1(t);    
      MyThread2 t2=new MyThread2(t);    
      t1.start();    
      t2.start();    
      }    
      }    
      ==========================    
         
      output:    
         
      5    
      10    
      15    
      20    
      25    
      100    
      200    
      300    
      400

Answer
You can create an object if you want a lock on it. E.g.

class table
{
  Object lock = new Object(); //created outside the method so it is the same for all threads.
  void printTable(int n)  
  {
      synchronized(lock)
      {
         ...
      }
  }
}

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.