线程安全与数据同步(下)
This Monitor 和 Class Moitor的详细介绍
this monitor
public class ThisMonitor
{
public synchronized void method1()
{
System.out.println(currentThread().getName() + " enter to method1");
try
{
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
public synchronized void method2()
{
System.out.println(currentThread().getName() + " enter to method2");
try
{
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ThisMonitor thisMonitor = new ThisMonitor();
new Thread(thisMonitor::method1, "T1").start();
new Thread(thisMonitor::method2, "T2").start();
}
}
public synchronized void method1()
{
System.out.println(currentThread().getName() + " enter to method1");
try
{
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
public void method2()
{
synchronized (this)
{
System.out.println(currentThread().getName() + " enter to method2");
try
{
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class monitor
public class ClassMonitor
{
public static synchronized void method1()
{
System.out.println(currentThread().getName() + " enter to method1");
try
{
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
public static synchronized void method2()
{
System.out.println(currentThread().getName() + " enter to method2");
try
{
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Thread(ClassMonitor::method1, "T1").start();
new Thread(ClassMonitor::method2, "T2").start();
}
}
程序死锁的原因以及如何诊断
程序死锁
交叉锁可导致程序出现死锁
内存不足
一问一答式的数据交换
数据库锁
文件锁
死循环引起的死锁
程序死锁举例
public class DeadLock
{
private final Object MUTEX_READ = new Object();
private final Object MUTEX_WRITE = new Object();
public void read()
{
synchronized (MUTEX_READ)
{
System.out.println(currentThread().getName() + " get READ lock");
synchronized (MUTEX_WRITE)
{
System.out.println(currentThread().getName() + " get WRITE lock");
}
System.out.println(currentThread().getName() + " release WRITE lock");
}
System.out.println(currentThread().getName() + " release READ lock");
}
public void write()
{
synchronized (MUTEX_WRITE)
{
System.out.println(currentThread().getName() + " get WRITE lock");
synchronized (MUTEX_READ)
{
System.out.println(currentThread().getName() + " get READ lock");
}
System.out.println(currentThread().getName() + " release READ lock");
}
System.out.println(currentThread().getName() + " release WRITE lock");
}
public static void main(String[] args)
{
final DeadLock deadLock = new DeadLock();
new Thread(() ->
{
while (true)
{
deadLock.read();
}
}, "READ-THREAD").start();
new Thread(() ->
{
while (true)
{
deadLock.write();
}
}, "WRITE-THREAD").start();
}
}
public class HashMapDeadLock
{
private final HashMap<String, String> map = new HashMap<>();
public void add(String key, String value)
{
this.map.put(key, value);
}
public static void main(String[] args)
{
final HashMapDeadLock hmdl = new HashMapDeadLock();
for (int x = 0; x < 2; x++)
new Thread(() ->
{
for (int i = 1; i < Integer.MAX_VALUE; i++)
{
hmdl.add(String.valueOf(i), String.valueOf(i));
}
}).start();
}
}
死锁诊断
交叉锁引起的死锁
Found one Java-level deadlock:
=============================
"WRITE-THREAD":
waiting to lock monitor 0x0000000009d1b0f8 (object 0x000000078b936800, a java.lang.Object),
which is held by "READ-THREAD"
"READ-THREAD":
waiting to lock monitor 0x0000000009d1b048 (object 0x000000078b936810, a java.lang.Object),
which is held by "WRITE-THREAD"
Java stack information for the threads listed above:
===================================================
"WRITE-THREAD":
at com.wangwenjun.concurrent.chapter04.DeadLock.write(DeadLock.java:33)
- waiting to lock <0x000000078b936800> (a java.lang.Object)
- locked <0x000000078b936810> (a java.lang.Object)
at com.wangwenjun.concurrent.chapter04.DeadLock.lambda$main$1(DeadLock.java:55)
at com.wangwenjun.concurrent.chapter04.DeadLock$$Lambda$2/791452441.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"READ-THREAD":
at com.wangwenjun.concurrent.chapter04.DeadLock.read(DeadLock.java:18)
- waiting to lock <0x000000078b936810> (a java.lang.Object)
- locked <0x000000078b936800> (a java.lang.Object)
at com.wangwenjun.concurrent.chapter04.DeadLock.lambda$main$0(DeadLock.java:47)
at com.wangwenjun.concurrent.chapter04.DeadLock$$Lambda$1/424058530.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
死循环引起的死锁(假死)