Always. But if you will use non-synch method inside synch method, then
you need to keep in mind, that threads can still access non-synch
method parallel directly.
synchronized void example() {
example1();
example2();
}
synchronized void example1() {
}
void example2() {
}
Thread 1 calls example;
Thread 2 can't call example, but can call example2 directly. Example:
public class Test extends Thread{
Test (String s) {
super(s);
}
synchronized void example() {
System.out.println("In example invoked from " + this.getName
());
example1();
example2();
}
synchronized void example1() {
System.out.println("In example1 invoked from " + this.getName
());
}
void example2() {
System.out.println("In example2 invoked from " + this.getName
());
}
public static void main(String [] args){
SerializeMyClassToBePersisted test = new
SerializeMyClassToBePersisted("A");
SerializeMyClassToBePersisted test2 = new
SerializeMyClassToBePersisted("B");
test.start();
test2.start();
}
@Override
public void run() {
if (this.getName().equals("A")) {
example();
} else {
example2();
}
}
}
Output:
In example invoked from A
In example2 invoked from B
In example1 invoked from A
In example2 invoked from A
On Nov 8, 3:11 pm, tikeswarmohanty mohanty
<[email protected]> wrote:
> Can any body tell me wheather i call non *Synchronized method within a
> **Synchronized
> method.
> Give me a example.
> *
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---