Home Answers Viewqa Java-Beginners doubt on synchronized block in java

 
 


adi shankara
doubt on synchronized block in java
0 Answer(s)      5 months and 16 days ago
Posted in : Java Beginners

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 Pages:
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
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 
Synchronized Threads
. Java's synchronized is used to ensure that only one thread is in a critical... block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. General form of synchronized block is: synchronized
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 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
Java Method Synchronized
Java Method Synchronized       The Java language Program supports multi threads. The synchronized... that help you in understanding Java Method Synchronized. We have a class Synchronized
Java synchronized method
Java synchronized method  What are synchronized methods and synchronized statements
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
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 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
Synchronized Threads
. Java's synchronized is used to ensure that only one thread is in a critical.... The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. General form of synchronized block
Synchronized Threads
. Java's synchronized is used to ensure that only one thread is in a critical... block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. General form of synchronized block is: synchronized
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
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
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
try and finally block
try and finally block  hello, If I write System.exit (0); at the end of the try block, will the finally block still execute?   hii, if we use System.exit (0); statement any where in our java program
big doubt
;html> <body> <%@page language="java" import="java.sql.*"%> <%@page language="java" import="java.io.*"%> <
"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 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
Please clarify my doubt
A { synchronized void foo(B b) { String name...(); } synchronized void last() { System.out.println("Inside A.last()"); } } class B { synchronized void bar(A a) { String
doubt this
doubt this  what is the use of "this" and "super" keyword
Doubt Regarding Charts
Doubt Regarding Charts  Hi, Can you please help me out by answering "hoe to include charts in core java code and struts code" thanks in advance, Swaroop Eswara   Please visit the following link: Jfreechart
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 ?
DOUBT ?  in c++ member function of a class must be public
doubt - Java Beginners
/java/example/java/applet Thanks
Doubt regarding charts and jsp
Doubt regarding charts and jsp  Hi, I successfully executed the bar chart in normal java application. But I want the Bar Chart to be executed... java application output to an jsp page? thanks in advance   Put the jar
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
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
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 ejb3 - EJB
doubt in ejb3  hi i am new to ejb3 .i have written simple code which... the persistence.xml in META-INF folder like these persistence.xml java...=PostgresDS' to JNDI name 'java:PostgresDS' 14:24:36,250 INFO [TomcatDeployment
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...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
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
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
servlet doubt on online examination system
servlet doubt on online examination system  hai. sir ,i am doing project in online examination system. taken back end as mysql database. and doing the programs using java servlet. MY PROBLEM and doubt is 1. how 2 skip

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.