This is related to threads. I have a class with synchronized
method m1(). Can I create different instances of this class
and execute the m1() for different threads?

Answer Posted / qim2010

Putting code inside a synchronized block makes the compiler
append instructions to acquire the lock on the specified
object before executing the code, and release it afterwards
(either because the code finishes normally or abnormally).
Between acquiring the lock and releasing it, a thread is
said to "own" the lock. At the point of Thread A wanting to
acquire the lock, if Thread B already owns the it, then
Thread A must wait for Thread B to release it. Thus in the
example below, simultaneous calls to increment() and
getCount() will always behave as expected; a read could not
"step in" while another thread was in the middle of
incrementing.

public class Counter {
private int count = 0;
public void increment() {
synchronized (this) {
count++;
}
}
public int getCount() {
synchronized (this) {
return count;
}
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How java enabled high performance?

824


What is boolean law?

749


Is it possible to write a regular expression to check if string is a number?

804


What is difference between == equals () and compareto () method?

764


What is java used for on a computer?

734


What is object cloning in Java?

880


Why static functions are used?

823


What are the types of java languages?

745


Hi i am creating desktop application in that i want calling to mobile number. i have java telephone api (JTAPI) but i dont understand how it configure & use plese help me

1616


What are class types in java?

835


What is sortedmap in java?

795


What are the differences between string, stringbuffer and stringbuilder?

766


What are the principle concepts of oops?

753


What is the use of flag?

872


How do you input a string in java?

776