/**
* 多线程之对象同步锁和异步锁Demo
*
* @author ietree
*/
public class SynAndAsynDemo {
public static void main(String[] args) {
final SynAndAsynDemo mo = new SynAndAsynDemo();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
mo.methodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
mo.methodB();
}
},"t2");
t1.start();
t2.start();
}
// 方法A
public synchronized void methodA(){
try {
System.out.println(Thread.currentThread().getName());
// 休眠4秒
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 方法B
public synchronized void methodB(){
System.out.println(Thread.currentThread().getName());
}
}