Java多线程学习--06

说明:参考《Java多线程核心技术》

10、静态和非静态的synchronized

关键字synchronized可以应用在static静态方法上,这样就相当于对当前的*.java文件对应的Class类进行加锁。

public class ThreadB extends Thread{

    @Override
    public void run(){
        StaticSynchronzied.printB();
    }
}
public class ThreadB extends Thread{

    @Override
    public void run(){
        StaticSynchronzied.printB();
    }
}
public class StaticSynchronzied {
    synchronized public static void  printA(){
        try {
            System.out.println("当前线程 " + Thread.currentThread().getName() + " start time = " + System.currentTimeMillis());
            Thread.sleep(2000);
            System.out.println("当前线程 " + Thread.currentThread().getName() + " end time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public static void printB() {
        System.out.println("当前线程 " + Thread.currentThread().getName() + " start time = " + System.currentTimeMillis());
        System.out.println("当前线程 " + Thread.currentThread().getName() + " end time = " + System.currentTimeMillis());
    }
}
public class StaticSynchronizedTest {
    public static void main(String[] args) {
        ThreadA threadA = new ThreadA();
        threadA.start();

        ThreadB threadB = new ThreadB();
        threadB.start();
    }
}
/**
 当前线程 Thread-0 start time = 1546699441060
 当前线程 Thread-0 end time = 1546699443073
 当前线程 Thread-1 start time = 1546699443073
 当前线程 Thread-1 end time = 1546699443073
 */

表面感觉没什么特别之处的,但是有本质的不同:synchronized关键字加在静态方法上相当于给Class类加锁,二加在非静态方法上时给对象加锁。

11、Class锁和对象锁
public class ThreadA extends Thread{
    private ClassLock classLock;

    public ThreadA(ClassLock classLock){
        super();
        this.classLock = classLock;
    }

    @Override
    public void run(){
        ClassLock.printA();  // Class 锁
    }
}
public class ThreadB extends Thread{

    private ClassLock classLock;

    public ThreadB (ClassLock classLock){
        super();
        this.classLock = classLock;
    }

    @Override
    public void run(){
        ClassLock.printB();  // Class 锁
    }
}
public class ThreadC extends Thread{
    private ClassLock classLock;

    public ThreadC(ClassLock classLock){
      super();
      this.classLock = classLock;
    }

    @Override
    public void run(){
      classLock.printC();  // 对象锁
    }
}
public class ClassLock {
    synchronized public static void  printA(){
        try {
            System.out.println("当前线程 " + Thread.currentThread().getName() + " start time = " + System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println("当前线程 " + Thread.currentThread().getName() + " end time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public static void printB() {
        System.out.println("当前线程 " + Thread.currentThread().getName() + " start time = " + System.currentTimeMillis());
        System.out.println("当前线程 " + Thread.currentThread().getName() + " end time = " + System.currentTimeMillis());
    }

    synchronized public void printC(){
        System.out.println("当前线程 " + Thread.currentThread().getName() + " start time = " + System.currentTimeMillis());
        System.out.println("当前线程 " + Thread.currentThread().getName() + " end time = " + System.currentTimeMillis());
    }
}
public class ClassLockTest {
    public static void main(String[] args) {
        ClassLock classLock = new ClassLock();
        ThreadA threadA = new ThreadA(classLock);
        threadA.start();
        ThreadB threadB = new ThreadB(classLock);
        threadB.start();
        ThreadC threadC = new ThreadC(classLock);
        threadC.start();
    }
}
/**
 当前线程 Thread-0 start time = 1546701241333
 当前线程 Thread-2 start time = 1546701241333
 当前线程 Thread-2 end time = 1546701241333
 当前线程 Thread-0 end time = 1546701242347
 当前线程 Thread-1 start time = 1546701242347
 当前线程 Thread-1 end time = 1546701242347
 */

异步的原因是因为持有不同对象的锁。一个是对象锁,另一个是Class锁。而Class锁可以对类的所有对象实例起作用。

12、死锁

不同的线程等待根本不可能被释放的锁,从而造成了所有的任务都在阻塞,无法完成

public class DeadLock implements Runnable {

    private String userName;
    public Object lock1 = new Object();
    public Object lock2 = new Object();

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public void run() {
        if (userName.equals("a")) {
            synchronized (lock1) {
                try {
                    System.out.println("userName = " + userName);
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock2) {
                    System.out.println("lock1---> lock2?");
                }
            }
        }

        if (userName.equals("b")) {
            synchronized (lock2) {
                try {
                    System.out.println("userName = " + userName);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock1) {
                    System.out.println("lock2---> lock1?");
                }
            }
        }
    }
}
public class DeadLockTest {
    public static void main(String[] args) {
        try {
            DeadLock deadLock = new DeadLock();
            deadLock.setUserName("a");
            Thread thread1 = new Thread(deadLock);
            thread1.start();

            Thread.sleep(1000);

            deadLock.setUserName("b");
            Thread thread2 = new Thread(deadLock);
            thread2.start();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
/**
 userName = a
 userName = b
 */

注意:死锁的产生和sleep的时间长短有关,如果sleep时间很短,ThreadA的sleep已经结束释放lock1后,ThreadB才开始,就无法产生死锁。如果ThreadA的sleep时间很长,一直占有lock2,就会产生死锁。这个需要好好理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值