public class TestSyn{
/*
* 线程同步:多个对象使用同一资源,当一个线程占有资源时,其他线程不能使用该资源,进入线程阻塞,等待使用线程释放资源
* 1.当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,
其他线程对“该对象”的该“synchronized方法”或者“synchronized代码块”的访问将被阻塞。
2.当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,
其他线程仍然可以访问“该对象”的非同步代码块。
3.当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,
其他线程对“该对象”的其他的“synchronized方法”或者“synchronized代码块”的访问将被阻塞。
*/
public static void main(String[] args) {
// tst1();//调用同一对象普通方法测试
// tst2();//调用同一对象同步方法测试
// tst3();//调用同一对象不同方法测试
// tst4();//调用同一对象不同同步方法测试
// tst5();//调用不同对象不同同步方法测试
// tst6();//调用不同对象静态同步方法测试
// tst7();//调用不同同步方法测试
}
public void test1() {
System.out.println("普通方法执行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("普通方法结束");
}
public synchronized void test2() {
System.out.println("同步方法执行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("同步方法结束");
}
public void test3() {
synchronized (this) {
System.out.println("同步块执行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("同步块结束");
}
}
public static synchronized void test4() {
System.out.println("静态同步方法执行");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("静态同步方法结束");
}
private static void tst7() {
TestSyn ts = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts.test2();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
TestSyn.test4();
}
}).start();
}
private static void tst6() {
TestSyn ts = new TestSyn();
TestSyn ts2 = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts2.test4();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ts.test4();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
TestSyn.test4();
}
}).start();
}
private static void tst5() {
TestSyn ts1 = new TestSyn();
TestSyn ts2 = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts1.test2();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ts2.test3();
}
}).start();
}
private static void tst4() {
TestSyn ts = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts.test2();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ts.test3();
}
}).start();
}
private static void tst3() {
TestSyn ts = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts.test1();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ts.test2();
}
}).start();
}
private static void tst2() {
TestSyn ts = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts.test2();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ts.test2();
}
}).start();
}
private static void tst1() {
TestSyn ts = new TestSyn();
new Thread(new Runnable() {
@Override
public void run() {
ts.test1();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ts.test1();
}
}).start();
}
}