Skip to content

Instantly share code, notes, and snippets.

@junwen12221
Created December 19, 2016 09:02
Show Gist options
  • Save junwen12221/162db65cb510679c25d4d38bced36a4e to your computer and use it in GitHub Desktop.
Save junwen12221/162db65cb510679c25d4d38bced36a4e to your computer and use it in GitHub Desktop.
锁的对象测试
package twelve;
public class TheThread {
public static void main(String[] args) {
ThreadLock_Util util = new ThreadLock_Util();
long start = System.currentTimeMillis();
testTmp1Tmp1(util);
Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println(System.currentTimeMillis() - start)));
}
/**
* 4s
* @param util
*/
public static final void testObject(ThreadLock_Util util) {
new Thread(() -> util.printObject1(Thread.currentThread().getId())).start();
new Thread(() -> util.printObject2(Thread.currentThread().getId())).start();
new Thread(() -> util.printThisObj1(Thread.currentThread().getId())).start();
new Thread(() -> util.printThisObj2(Thread.currentThread().getId())).start();
}
/**
* 2s
* @param util
*/
public static final void testStaticAndClass(ThreadLock_Util util) {
new Thread(() -> util.printStatic2(Thread.currentThread().getId())).start();
new Thread(() -> util.printStaticWithClass(Thread.currentThread().getId())).start();
}
/**
* 1s
* @param util
*/
public static final void testStaticAndTmp(ThreadLock_Util util) {
new Thread(() -> util.printTmpObj1(Thread.currentThread().getId())).start();
new Thread(() -> util.printStatic2(Thread.currentThread().getId())).start();
}
/**
* 1s
* @param util
*/
public static final void testStaticAndObject(ThreadLock_Util util) {
new Thread(() -> util.printThisObj1(Thread.currentThread().getId())).start();
new Thread(() -> util.printStatic2(Thread.currentThread().getId())).start();
}
/**
* 1s
* @param util
*/
public static final void testTmpAndObject(ThreadLock_Util util) {
new Thread(() -> util.printThisObj1(Thread.currentThread().getId())).start();
new Thread(() -> util.printTmpObj1(Thread.currentThread().getId())).start();
}
/**
* 1s
* @param util
*/
public static final void testTmp1Tmp1(ThreadLock_Util util) {
new Thread(() -> util.printTmpObj1(Thread.currentThread().getId())).start();
new Thread(() -> util.printTmpObj1(Thread.currentThread().getId())).start();
}
}
class ThreadTest extends Thread {
private ThreadLock_Util thread;
public ThreadTest(ThreadLock_Util t9) {
this.thread = t9;
}
@Override
public void run() {
thread.printObject1(Thread.currentThread().getId());
System.out.println(Thread.currentThread().getId() + " end");
}
}
class ThreadLock_Util {
/**
* 验证锁的是this指向的对象,当前对象
*
* @param l
*/
public synchronized void printObject1(long l) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 验证锁的是this指向的对象,当前对象
*
* @param l
*/
public synchronized void printObject2(long l) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 验证锁的是本类
*
* @param l
*/
public static synchronized void printStatic1(long l) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 验证锁的是本类
*
* @param l
*/
public static synchronized void printStatic2(long l) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 验证锁的是本类
*
* @param l
*/
public void printStaticWithClass(long l) {
synchronized (this.getClass()) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 验证锁的是临时对象
*
* @param l
*/
public void printTmpObj1(long l) {
synchronized (new Object()) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 验证锁的是临时对象
*
* @param l
*/
public void printTmpObj2(long l) {
synchronized (new Object()) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 验证锁的是this指向的对象,当前对象
*
* @param l
*/
public void printThisObj1(long l) {
synchronized (this) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 验证锁的是this指向的对象,当前对象
*
* @param l
*/
public void printThisObj2(long l) {
synchronized (this) {
System.out.println(l);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment