doubt on synchronized block in java

Ads
 

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

Ads









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
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 beginners doubt!
java beginners doubt!  How to write clone()in java strings
java doubt
use throws keyword. Point to note here is that the Java compiler very well knows
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
doubt this
doubt this  what is the use of "this" and "super" keyword
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
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
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
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
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
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
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
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
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
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
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
Executing anonymous pl/sql block and get resultset in Java
Executing anonymous pl/sql block and get resultset in Java  Hello All, I would like to execute the anonymous pl sql block and need to get... block. But the pl sql block itself will come from the database as text. So i
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
Validation doubt
Validation doubt  hi..... thanks for the other validation code. I have got that and implemented in my code but i have a doubt in that. As we try... think i am able to tell u what i want to and u have got whats my doubt. plz give
doubt on DAO's
doubt on DAO's  hai frnds.... can anyoneexplain about how to integrate struts with hibernate,any predifined plugin is available or we need to create our own plugin????? and please help me. how to use dao s while integrating

Ads