In two different situations
1): Non-Synchronized method to enter this object
Answer: You can
2): The synchronization method to enter this object
Answer: No, you can't.
The first case of the original code
/**
* * *
/package thread;
/**
* @author Administrator
*/public
class TestClass {
/**
* * @param
args
* * public static void Main (string[] args) {
TestClass TC = new TestClass ();
Thread1 T1 = tc.new Thread1 (TC);
T1.start ();
Thread2 t2 = tc.new Thread2 (TC);
T2.start ();
}
Class Thread1 extends thread{
testclass tc = NULL;
Public Thread1 (TestClass tc) {
THIS.TC = TC;
}
@Override public
Void Run () {
tc.method1 ();
}
}
Class Thread2 extends thread{
testclass tc = NULL;
Public Thread2 (TestClass tc) {
THIS.TC = TC;
}
@Override public
Void Run () {
//TODO auto-generated method Stub
tc.method2 ();
}
Public synchronized void Method1 () {
System.out.println ("method1");
try {
thread.sleep (1000*10);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
} Public void Method2 () {
System.out.println ("Method2");
}
The second case is the original code
/**
* * *
/package thread;
/**
* @author Administrator
*/public
class TestClass {
/**
* * @param
args
* * public static void Main (string[] args) {
TestClass TC = new TestClass ();
Thread1 T1 = tc.new Thread1 (TC);
T1.start ();
Thread2 t2 = tc.new Thread2 (TC);
T2.start ();
}
Class Thread1 extends thread{
testclass tc = NULL;
Public Thread1 (TestClass tc) {
THIS.TC = TC;
}
@Override public
Void Run () {
tc.method1 ();
}
}
Class Thread2 extends thread{
testclass tc = NULL;
Public Thread2 (TestClass tc) {
THIS.TC = TC;
}
@Override public
Void Run () {
//TODO auto-generated method Stub
tc.method2 ();
}
Public synchronized void Method1 () {
System.out.println ("method1");
try {
thread.sleep (1000*10);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
} Public synchronized void Method2 () {
System.out.println ("Method2");
}