多线程同步,解决的是多线程安全性问题,避免获取错误的资源,但同步也同时会带来性能损耗和线程死锁的问题.多线程同步采用了同步代码块和同步方法的方式,依靠的是锁机制实现了互斥访问.因为互斥访问,所以不能并行处理,存在性能问题.如果出现了线程死锁,那可能直接导致程序众多的线程都处于阻塞状态,无法继续运行.
下面通过一个案例演示线程死锁,代码如下:
package com.lanqiao.homework201704201;
public class DeadLockThead {
//创建两个线程之间竞争使用的对象
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new Thread(new ShareThread1()).start();
new Thread(new ShareThread2()).start();
}
private static class ShareThread1 implements Runnable{
public void run() {
synchronized(lock1){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock2){
System.out.println("ShareThread1");
}
}
}
}
private static class ShareThread2 implements Runnable{
public void run() {
synchronized(lock2){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock1){
System.out.println("ShareThread2");
}
}
}
}
}
//创建两个线程之间竞争使用的对象
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new Thread(new ShareThread1()).start();
new Thread(new ShareThread2()).start();
}
private static class ShareThread1 implements Runnable{
public void run() {
synchronized(lock1){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock2){
System.out.println("ShareThread1");
}
}
}
}
private static class ShareThread2 implements Runnable{
public void run() {
synchronized(lock2){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock1){
System.out.println("ShareThread2");
}
}
}
}
}
上面的代码中,两个线程之间竞争使用对象lock1和lock2,在run方法中分别握着第一把锁,等待第二把锁,程序死锁.
解决的方法是多个线程以同样的顺序获取锁,不出现交叉就不会出现死锁的问题.
修改后的代码如下:
package com.lanqiao.homework201704201;
public class DeadLockThead {
//创建两个线程之间竞争使用的对象
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new Thread(new ShareThread1()).start();
new Thread(new ShareThread2()).start();
}
private static class ShareThread1 implements Runnable{
public void run() {
synchronized(lock1){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock2){
System.out.println("ShareThread1");
}
}
}
}
private static class ShareThread2 implements Runnable{
public void run() {
synchronized(lock1){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock2){
System.out.println("ShareThread2");
}
}
}
}
}
//创建两个线程之间竞争使用的对象
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new Thread(new ShareThread1()).start();
new Thread(new ShareThread2()).start();
}
private static class ShareThread1 implements Runnable{
public void run() {
synchronized(lock1){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock2){
System.out.println("ShareThread1");
}
}
}
}
private static class ShareThread2 implements Runnable{
public void run() {
synchronized(lock1){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(lock2){
System.out.println("ShareThread2");
}
}
}
}
}
这就需要我们编码时注意思考的问题,避免程序发生死锁现象.