doubt on synchronized block in java

doubt on synchronized block in java

Hi ! some people are feeling bore on my regular questions on threads. Do not feel bore on my regular questions.I am a beginner.I am learning java with out any teacher.I need your valuable suggestinons.Today i am going to ask on synchronized block.

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 based on 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
View Answers









Related Tutorials/Questions & Answers:
doubt on synchronized block in java
doubt on synchronized block in java  Hi ! some people are feeling... suggestinons.Today i am going to ask on synchronized block. the general form of synchronized block is class table { ....... void printTable(int n
Is catch(){} block synchronized?
Is catch(){} block synchronized?  The code in catch(){} block behaves synchronized. In one block, if I do {write to file1; write to file2}, and in another {write to file2; write to file1}, they deadlock. Is this implicit sync
Advertisements
Using a synchronized block
Description: This tutorial demonstrate how to implement synchronized block. In multithreading application a synchronized block is used to acquires the lock for an object. Code: class Called 
Java synchronized method
Java synchronized method  What are synchronized methods and synchronized statements
Java Synchronized
Java Synchronized       The keyword synchronized entitled to the method or block is used... to allow execution of block of code to be synchronized on the lock
Synchronized - Java Interview Questions
Synchronized  i want synchronized programs ? in java plz help me?any site   Hi Friend, If you want to know about the synchronized method,please visit the following link: http://www.roseindia.net/java/java-methods
synchronized Java Keyword
synchronized Java Keyword       The synchronized is a keyword defined in the java programming... relevant to a compiler in java programming language likewise the synchronized keyword
Synchronized with example - Java Beginners
Synchronized with example  Hi Friends, I am beginner in java. what i know about synchonized keyword is,If more that one 1 thread tries to access a particular resource we can lock the method using synchronized keyword. Then after
Java Method Synchronized
Java Method Synchronized       The Java language Program supports multi threads. The synchronized... a code that help you in understanding Java Method Synchronized. We have a class
Java Function for block inside a block
Java Function for block inside a block  Write a function in Java that attempts to place a set of squares of varying widths into another, larger square. If there is no possible layout, return undefined. Otherwise, return
Java Function for block inside a block
Java Function for block inside a block  Write a function in Java that attempts to place a set of squares of varying widths into another, larger square. If there is no possible layout, return undefined. Otherwise, return
java doubt
use throws keyword. Point to note here is that the Java compiler very well knows
java beginners doubt!
java beginners doubt!  How to write clone()in java strings
URL Block - Java Beginners
URL Block  Hello sir, How to block one website using java.for example if we want block "www.orkut.com" site,how to block this site using java... to block a URL like this?please help me.. Thanking you
doubt this
doubt this  what is the use of "this" and "super" keyword
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
Synchronized Threads
in a sequential manner. Java's synchronized is used to ensure that only one thread.... The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. General form of synchronized block
"Doubt on Swing" - Java Beginners
"Doubt on Swing"  Hi Friend.... Thanks for ur goog Response.. i need to create a GUI Like... pic1.gif RadioButton pic2.gif RadioButton Pic3.gif RadioButton If we have select d appropriate radio
Doubt
Doubt  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details
Doubt on Segmentation - Java Beginners
Doubt on Segmentation  Hi .. ImageProcessing.. How to segment the each and every characters from the bitmap image Consider for an example bitmap1.bmp: Visuallc++ text is on bitmap1.bmp..How to extract those from image
When finally block is not executed in Java?
When finally block is not executed in Java?  Hi, I have read that finally block is always executed in Java. But I want to know if there is any conditions when finally is not executed. When finally block is not executed in Java
When finally block is not executed in Java?
When finally block is not executed in Java?  Hi, I have read that finally block is always executed in Java. But I want to know if there is any conditions when finally is not executed. When finally block is not executed in Java
I'v a doubt - Java Beginners
I'v a doubt  Hai to all, How to break the mysql jar file, and import into the java file by without using any editiors. With regards, Terrance. J
java doubt - Java3D
java doubt   hi sir , this is sairam am intersted on programs. so i choose the java . i want to become a good programmer . in java there are so many catagerious , on that which one is use full to me for improving me
doubt - Java Beginners
/java/example/java/applet Thanks
Doubt
User request form  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details.   1)page1.html: <html> <form type=get action="page2.html
Doubt
How to load page  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details.   1)page1.html: <html> <form type=get action="page2.html
Doubt
Submit and process form  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details.   1)page1.html: <html> <form type=get action
Doubt
load next page after submitting  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details.   1)page1.html: <html> <form type=get action
DOUBT
DOUBT  JAVA IS PLATFORM INDEPENDENT BUT U HAVE ALSO WRITTEN THAT JAVA RUN ON ANY JAVA PLATFORM. SO, WHAT DOES THIS ACTUALLY MEAN THAT WHETHER JAVA CAN RUN ANYWHERE OR CAN RUN ANYWHERE WHERE JAVA PLATFORM
doubt
applications and Is java pure object oriented programming language and from where we can..., object oriented language is required in real time applications. Java is not pure... creating the instance. You can download the java decompiler from the following link
Doubt
Doubt  Hi, Can any one help me out in step wise, how to get are charts using java in eclipse ide? thanks in advance   Download jar...)Select 'Java Build Path' in the left hand column. 2)Select the 'Libraries' tab
Hi Friend.. Doubt on + - Java Beginners
Hi Friend.. Doubt on +   Hi friend... import java.io.*; class Plus { public static void main(String args[]) { int a=10; int b= 25... } }   Hi friend, The Java language provides special support
Doubt on Data Types - Java Beginners
Doubt on Data Types  Hi Friend doubt on DataTypes... How should i declare n Assign value to the Float variable; Can u Please COMPILE this Program... to put it in java.  If you want to declare a decimal value in Java
Hi Friend.. Doubt on + - Java Beginners
Hi Friend.. Doubt on +   Hi friend... import java.io.*; class Plus { public static void main(String args[]) { int a=10; int b= 25...;java Plus A = 10 B = 25 Plus = 1025
doubt in inheritance program - Java Beginners
doubt in inheritance program  how will we get the result 6 2 5 in the inheritance program in the given example i got 6 &2 but i am confused about 5
Hi...doubt on Packages - Java Beginners
Hi...doubt on Packages  Does import.javax.mail.* is already Existing Package in java.. I have downloaded one program on Password Authentication... ..Explain me.  Hi friend, Package javax.mail The Java Mail API allows
Hi ..doubt on DATE - Java Beginners
Hi ..doubt on DATE  Hi Friend...Thank u for ur valuable response.. IS IT POSSIBLE FOR US? I need to display the total Number od days by Each month... WAIT I WILL SHOW THE OUTPUT: --------------------------------- ENTER
Hi..Again Doubt .. - Java Beginners
Hi..Again Doubt ..  Thank u for ur Very Good Response...Really great.. i have completed that.. If i click the RadioButton,,ActionListenr should get call. It should add to the MS Acess table..Plz check this out.... hope u ill
doubt in my program code - Java Beginners
doubt in my program code  i have developed a web browser with the help of standard widget toolkit(swt) and java. i creted some buttons such as GO,REFRESH,STOP.... on which the name is not apppeared as GO,STOP...but the name
doubt in the following code of java - Java Beginners
doubt in the following code of java  Hi frends, actually i want to display the following calculated matrix a[][] in the table format... which can accept all the size of matrix........and one more doubt, i want
Hi Friend ..Doubt on Exceptions - Java Beginners
Hi Friend ..Doubt on Exceptions  Hi Friend... Can u please send some Example program for Exceptions.. I want program for ArrayIndexOutOfbounds OverFlow Exception.. Thanks... Sakthi  Hi friend, Code
Java Code to Block a website accessed by any browser in windows
Java Code to Block a website accessed by any browser in windows  I want create an application in Java through Netbean IDE to block any website to be accessed by any browser. I also want to display only its html contents. please
My big doubt on java prgmn.. need solution asap
My big doubt on java prgmn.. need solution asap  How do you write prgrm 4 dis : 3% discount is given if payment is made within 30days of purchase. date of purchase and payment entered by user
how to ajax components are synchronized
how to ajax components are synchronized  How to ajax components are synchronized   Hi, Ajax consist of set of technologies to get the data from server Asynchronously.ADS_TO_REPLACE_1 It is the object of var xmlHttp
Synchronized Threads
in a sequential manner. Java's synchronized is used to ensure that only one thread... the native lock. The synchronized block allows execution of arbitrary code... block is: synchronized (object reference expression)   
Synchronized Threads
in a sequential manner. Java's synchronized is used to ensure that only one thread.... The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. General form of synchronized block
finally block
finally block  hii, If I am writing return at the end of the try block and some code in finally block, then the finally block will execute??ADS_TO_REPLACE_1   hello, certainly finally block will execute
Statement block
Statement block  What is the purpose of a statement block
ModuleNotFoundError: No module named 'synchronized-set'
ModuleNotFoundError: No module named 'synchronized-set'  Hi, My... named 'synchronized-set' How to remove the ModuleNotFoundError: No module named 'synchronized-set' error? Thanks   Hi, In your

Ads