Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


adi shankara
doubt on synchronized block in java
0 Answer(s)      2 years 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

Advertisement
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 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 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 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 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
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
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 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
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
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
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
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
Advertisements
 

 

 

DMCA.com