synchronized 锁String, 非final , 如果String的value值改变了会怎么样.
synchronized 锁Integer, 非final, 如果Integer 值都是1 , 多个线程并发会怎么样.
synchronized 锁Integer, 非final, 如果Integer 值都是128 , 多个线程并发会怎么样.
String 值改变的话, 原有的锁依旧处于加锁状态. 新的线程在申请锁的时候, 会加载到新的String 值. 导致锁失效, 新的线程依旧会执行.
因为Integer类中有缓存 -128 到 127 , 如果值相同, 会导致所有的线程阻塞, 顺序执行.
如果锁的值 超出 -128 ~127 的范围. 并发执行.
/**
* 验证锁启动顺序,
* 当变更lock的时候, 会有两个线程同时存在.
*/
public class lockString01 {
private static String lock = "lock";
public static void main(String[] args) throws Exception {
doRun("t1");
doRun("t2");
doRun("t3");
doRun("t4");
doRun("t5");
// System.out.println("chang lock.....");
lock = "new lock";
// System.out.println("chang lock.....");
doRun("t6");
doRun("t7");
// while (true) {
// lock = UUID.randomUUID().toString();
// try {
// TimeUnit.SECONDS.sleep(1L);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
private static void doRun(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("thread: " + name + " start");
try {
System.out.println("thread: " + name + " sleep. lock is :" + lock);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread: " + name + " end");
}
}
}).start();
}
}
/**
* 验证锁变换, 是否是顺序加锁. 目前看是,存在并发
*/
public class lockString02 {
private static String lock = "lock";
public static void main(String[] args) throws Exception {
doRun("t1");
doRun("t2");
doRun("t3");
doRun("t4");
doRun("t5");
doRun("t6");
doRun("t7");
while (true) {
lock = UUID.randomUUID().toString();
}
}
private static void doRun(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("thread: " + name + " start");
try {
System.out.println("thread: " + name + " sleep. lock is :" + lock);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread: " + name + " end");
}
}
}).start();
}
}
/**
* 锁随机变换.
* 每个线程休眠5s, 阻塞1s再去加载下一个线程.
* 验证结果:
* 锁失效了, 存在并发, 线程会有并发执行.
*
*/
public class lockString03 {
private static String lock = "lock";
public static void main(String[] args) throws Exception {
doRun("t1");
doRun("t2");
doRun("t3");
doRun("t4");
doRun("t5");
doRun("t6");
doRun("t7");
while (true) {
lock = UUID.randomUUID().toString();
}
}
private static void doRun(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("thread: " + name + " start");
try {
System.out.println("thread: " + name + " sleep. lock is :" + lock);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread: " + name + " end");
}
}
}).start();
try {
System.out.println("thread: " + name + " block 1 s.");
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 验证锁变换, 是否是顺序加锁. 目前看是,存在并发
*/
public class lockInteger01 {
private static Integer lock = 1;
public static void main(String[] args) throws Exception {
doRun("t1");
doRun("t2");
doRun("t3");
doRun("t4");
doRun("t5");
lock = 2;
doRun("t6");
doRun("t7");
// while (true) {
// lock = UUID.randomUUID().toString();
// try {
// TimeUnit.SECONDS.sleep(1L);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
private static void doRun(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("thread: " + name + " start");
try {
System.out.println("thread: " + name + " sleep. lock is :" + lock);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread: " + name + " end");
}
}
}).start();
}
}
/**
* 验证锁变换, 是否是顺序加锁. 目前看是,存在并发
*/
public class lockInteger02 {
private static Integer lock = 1;
public static void main(String[] args) throws Exception {
doRun("t1");
doRun("t2");
lock = 2;
doRun("t3");
doRun("t4");
doRun("t5");
doRun("t6");
doRun("t7");
while (true) {
lock = new Random().nextInt();
}
}
private static void doRun(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("thread: " + name + " start");
try {
System.out.println("thread: " + name + " sleep. lock is :" + lock);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread: " + name + " end");
}
}
}).start();
}
}
/**
* 验证Integer值为1, 两个锁. 多个线程会不会并发执行
* 结果: 不会并发线程, 所以使用Integer 相同的值会有问题. 接下来验证128
*/
public class lockInteger03 {
private static Integer lock01 = 1;
private static Integer lock02 = 1;
public static void main(String[] args) throws Exception {
doRunByLock01("1-t1");
doRunByLock01("1-t2");
doRunByLock01("1-t3");
doRunByLock01("1-t4");
doRunByLock01("1-t5");
doRunByLock01("1-t6");
doRunByLock01("1-t7");
doRunByLock02("2-t1");
doRunByLock02("2-t2");
doRunByLock02("2-t3");
doRunByLock02("2-t4");
doRunByLock02("2-t5");
doRunByLock02("2-t6");
doRunByLock02("2-t7");
}
private static void doRunByLock01(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock01) {
System.out.println("thread lock 01 : " + name + " start");
try {
System.out.println("thread lock 01 : " + name + " sleep. lock is :" + lock01);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread lock 01 : " + name + " end");
}
}
}).start();
}
private static void doRunByLock02(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock02) {
System.out.println("thread lock 02 : " + name + " start");
try {
System.out.println("thread lock 02 : " + name + " sleep. lock is :" + lock02);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread lock 02 : " + name + " end");
}
}
}).start();
}
}
/**
* 验证Integer值为128, 两个锁. 多个线程会不会并发执行
* 结果: 会并发执行, Inter值如果大于 128 会并发执行.
* -128 <= lock <= 127 之间会顺序执行, 大于128会并发执行
*/
public class lockInteger04 {
private static Integer lock01 = -129;
private static Integer lock02 = -129;
public static void main(String[] args) throws Exception {
doRunByLock01("1-t1");
doRunByLock01("1-t2");
doRunByLock01("1-t3");
doRunByLock01("1-t4");
doRunByLock01("1-t5");
doRunByLock01("1-t6");
doRunByLock01("1-t7");
doRunByLock02("2-t1");
doRunByLock02("2-t2");
doRunByLock02("2-t3");
doRunByLock02("2-t4");
doRunByLock02("2-t5");
doRunByLock02("2-t6");
doRunByLock02("2-t7");
}
private static void doRunByLock01(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock01) {
System.out.println("thread lock 01 : " + name + " start");
try {
System.out.println("thread lock 01 : " + name + " sleep. lock is :" + lock01);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread lock 01 : " + name + " end");
}
}
}).start();
}
private static void doRunByLock02(String name) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock02) {
System.out.println("thread lock 02 : " + name + " start");
try {
System.out.println("thread lock 02 : " + name + " sleep. lock is :" + lock02);
TimeUnit.SECONDS.sleep(5L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread lock 02 : " + name + " end");
}
}
}).start();
}
}