You are here:

Java/synchronization block

Advertisement


Question
Hi sir! my doubt is on synchronized block in java
-------------------------------------------------
the general form of synchronized block is
class table
{
.......
void printTable(int n)
{
synchronized(object)
{
......
}
}
}
--------------------------------------------
1)Here object means object of any class.i.e we can lock object of any class besides the object of  "table" class.
2)if we place this in place of object it is possible to lock object of table class
3)then how can we lock object of a class other than table?can you give example  based on below programme?
4) if we want to access a variable or method from table class we need object of table .
if we lock object of table class it is not possible to an object of table class to access synchronized block of table class
from two different places simultaneously.
but if we lock object of another class,say X,how can the object of class X can access synchronized block of table class?
because object of class X is not object of table.object of table only can access members of table.
------------------------------------------------------------------
I think you got my doubt.
I request you to clarify my doubt.
-------------------------------------------------------------------------
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
500

Answer
Synchronizing an object does not prevent access to that object. All it does is ensure that any other code also synchronizing on that same object will not execute those synchronization block at the same time. Any code not in a synchronized block will continue to execute regardless of any synchronization in place.

In order to prevent other code from interfering with synchronization it is common to have a private object that is used for synchronization.  

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.