package creative.fire.multithread;
import java.util.concurrent.CountDownLatch;
/**
* @author feuyeux@gmail.com 2011-10-13
*
*/
/*
* synchronized static void foo() { ... } is the same as
*
* static void foo() { synchronized(SomeClass.class) { ... } }
*
* synchronized void foo() { ... } is the same as
*
* void foo() { synchronized(this) { ... } }
*/
public class LockMethodTest {
private static final int timeout = 3 * 1000;
public synchronized void a(String name) throws InterruptedException {
System.out.println(name + " enter a");
Thread.sleep(timeout);
System.out.println("quit a");
}
public synchronized void b() throws InterruptedException {
System.out.println("enter b");
Thread.sleep(timeout);
System.out.println("quit b");
}
public synchronized static void c() throws InterruptedException {
System.out.println("enter c");
Thread.sleep(timeout);
System.out.println("quit c");
}
public static void main(String[] args) throws InterruptedException {
testInSameObject();
System.out.println("--------");
testInTwoObjects();
System.out.println("--------");
testClassObject();
}
private static void testClassObject() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(2);
final LockMethodTest thiz = new LockMethodTest();
new Thread(new Runnable() {
@Override
public void run() {
try {
LockMethodTest.c();
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
thiz.a("object");
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}).start();
latch.await();
}
private static void testInTwoObjects() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(2);
final LockMethodTest thiz1 = new LockMethodTest();
final LockMethodTest thiz2 = new LockMethodTest();
new Thread(new Runnable() {
@Override
public void run() {
try {
thiz1.a("object1");
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
thiz2.a("object2");
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}).start();
latch.await();
}
private static void testInSameObject() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(2);
final LockMethodTest thiz = new LockMethodTest();
new Thread(new Runnable() {
@Override
public void run() {
try {
thiz.a("object");
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
thiz.b();
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}).start();
latch.await();
}
}
分享到:
相关推荐
《童虎学习笔记》SQL语言高手样本数据websites.sql
《童虎学习笔记》SQL语言高手样本数据apps.sql
《童虎学习笔记》SQL语言高手样本数据access_log.sql
《童虎学习笔记》mysql 8.0.28 mgr 监控视图 gr_member_routing_candidate_status
人事管理系统.rar
(6)互动论坛:注册登录后的童虎可以进行发帖,在此可以进行查看各种帖子,以及回复跟帖等等一些列操作,可以在里面进行一些自由友善的交流。 (7)系统概要:有联系方式,提供本系统的一些作者的联系方式以及工作...
(6)互动论坛:注册登录后的童虎可以进行发帖,在此可以进行查看各种帖子,以及回复跟帖等等一些列操作,可以在里面进行一些自由友善的交流。 (7)系统概要:有联系方式,提供本系统的一些作者的联系方式以及工作...