深入多线程2

2017-08-21  本文已影响4人  安安静静写代码

死锁
概念: 两个线程互相拿着对方手中的锁不释放并且还想要对方手中的锁 这种情况称之为死锁
条件:两个线程 两个不同的锁每个线程都必须用这两把锁

package com.qf.demo2;
import java.awt.Color;
public class Test {

    public static void main(String[] args) {
            Ni ni = new Ni();
            Girl girl = new Girl();
            
            ni.start();
            girl.start();
    }
}
class Ni extends Thread{
    
    @Override
    public void run() {
        synchronized (Clock.A) {           // 锁  A
            System.out.println("你拿到了筷子A,还想要筷子B");
            synchronized (Clock.B) {   // 锁  B
                System.out.println("你也拿到了筷子B , 吃 , 吃完以后 恢复单身狗");
            }
        }
    }
}
class Girl extends Thread{
    @Override
    public void run() {
        synchronized (Clock.B) {
            System.out.println("女朋友拿到了筷子B, 还想要筷子A");
            synchronized (Clock.A) {
                System.out.println("女朋友也拿到了A, 吃, 吃完以后回家睡觉");
            }
        }
    }
}
class Clock{
    public static final Object  A = new Object();
    public static final Object B = new Object();
}

死锁的结果:
你拿到了筷子A,还想要筷子B
女朋友拿到了筷子B, 还想要筷子A

线程间通信
线程之间虽说是相互独立的,但是有时线程之间是需要互相通信的.例如男朋友存钱,女朋友取钱,这两个过程属于两个线程,但是必须男朋友存上钱之后女朋友才能取钱,所以需要判断各自线程运行的条件,如果条件不符合则通知另一个线程去运行,这就称之为线程间通信

简单来说,线程间通信是通过 wait() notify() notifyAll() 这三个函数来实现的
wait(): 让当前线程进入阻塞状态
notify() :唤醒因为调用wait方法进入到阻塞状态的线程中的随机一个(如果有两个的话在A线程中调用该方法 则唤醒的是B 线程)
notifyAll: 唤醒所有因为调用wait方法进入到阻塞状态的线程

线程间通信,线程必须是安全的,所以要进行代码同步.即使用synchronized代码块(同步代码块)或synchronized方法(同步方法)

上述三个函数由对象资源调用,即锁 用锁调用

下面举个例子

package com.qf.demo3;
/**
 * 需求:
 * A12B34C56......Z5152
 * 
 * 两个线程
 * 一个字母 
 * 一个数字
 * 
 *用到 线程间通信
 *
 *wait
 *notify
 *
 *
 *money  ==1000  == 0
 *
 *自己写一个标志位
 *  flag = true  打印了数字了还没打印字母, 此时 应该 让数字等着   打印 字母
 *         false  打印了字母了 还没打印数字,此时 应让 字母等着, 打印数字
 *  
 */
public class Test3 {

    public static void main(String[] args) {
        Resurce resurce = new Resurce();
        
        Number number = new Number(resurce);
        Letter letter = new Letter(resurce);
        
        Thread thread = new Thread(number);
        Thread thread2 = new Thread(letter);
        
        thread.start();
        thread2.start();
    }
}



//true 打印了数字 还没打印字母
// false  打印了 字母还没有打印数字



class Resurce {
    boolean flag = true;   //true 打印了数字 还没打印字母
                            // false  打印了 字母还没有打印数字
    public synchronized void printNumber(int num){
        // 1判断是否应该wait
        if(flag == true){
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 如果没有进入wait 目前来说是false 应该打印数字
        System.out.print(num+""+(num+1));//
        //改变状态
        flag = true;
        this.notify();
    }
    public synchronized void printLetter(int letter){
        if(flag == false){
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 如果没有进入 wait 证明现在是true ,应该打印字母
        System.out.print((char)letter);
        // 已经打印过了字母了. 应该吧状态改变成为false
        flag = false;
        this.notify();
    }
}

class Number implements Runnable{
    Resurce resurce;
    public Number(Resurce resurce) {
        this.resurce = resurce;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 52; i+=2) {
            resurce.printNumber(i);
        }
    }
}

class Letter implements Runnable{
    Resurce resurce;
    public Letter(Resurce resurce) {
        this.resurce = resurce;
    }
    @Override
    public void run() {
        for (int i = 0; i < 26; i++) {
            resurce.printLetter(65+i);
        }
    }
}


sleep()和wait()的区别

sleep() 释放cpu没有释放锁,进入阻塞状态,时间到了就到达了就绪状态
wait() 释放了cpu也释放了锁,进入阻塞状态后必须通过notify()或者notifyAll()唤醒才能进入到就绪状态

上一篇 下一篇

猜你喜欢

热点阅读