java多进程 -CD7-孙鑫-(5)

本节主要介绍线程的死锁。和如何终止线程
每个class也有一个锁,是这个class所对应的Class对象的锁。
wait、notify、notifyAll
1.每一个对象除了有一个之外,还有一个等待队列(wait set),当一个对象刚创建的时候,它的对待队列是空的。
2.我们应该在当前线程锁住对象的锁后,去调用该对象的wait方法。也就是说wait()和notify()需要在同步方法和同步块中。
3.当调用对象的notify方法时,将从该对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程。
4.当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待的线程,这些线程将成为可运行的线程。
5.wait和notify主要用于producer-consumer这种关系中。
对于同步方法使用的是this对象的监视器wait方法和notify方法调用的是this对象的等待队列

生产者消费者示例:

package Test;

class Test {
    public static void main(String[] args) {
        Queue q = new Queue();
        Produce p = new Produce(q);
        Consumer c = new Consumer(q);
        p.start();
        c.start();
    }
}

// 生产者线程类
class Produce extends Thread {
    Queue q;

    Produce(Queue q) {
        this.q = q;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            q.put(i);
            System.out.println("Product i " + i);
        }
    }
}

// 消费者线程类
class Consumer extends Thread {
    Queue q;

    Consumer(Queue q) {
        this.q = q;
    }

    public void run() {
        while (true) {
            System.out.println("Consumer get" + q.get());
        }
    }
}

class Queue {
    int value;
    // 判断队列是否是满的,如果是满的说明数据没有被消费者获取
    boolean bFull = false;
        public synchronized void put(int i) {
                if (!bFull) {
                    value = i;
                    bFull = true;
                    notify();
                }
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

//就消费者而言
    public synchronized int get() {
        //如果队列有值则返回
        if(!bFull)
        {
            try {
                //消费者需要等待
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        bFull = false;
        //通知生产者继续放置数据
        notify();
        return value;
    }
}

结果

Consumer get0
Product i 0
Consumer get1
Product i 1
Consumer get2
Product i 2
Consumer get3
Product i 3
Consumer get4
Product i 4
Consumer get5
Product i 5
Consumer get6
Product i 6
Consumer get7
Product i 7
Consumer get8
Product i 8
Consumer get9
Product i 9

线程的终止
设置一个flag变量。
结合interrupt()方法
示例:

package ThreadStop;

class ThreadStopTest {
    public static void main(String[] args)
    {
        Thread1 t = new Thread1();
        t.start();
        int index = 0;
        while(true)
        {
            if(index++ == 500)
            {
                t.stopStop();
                break;
            }
            System.out.println(Thread.currentThread().getName());
        }
        System.out.println("main exit");
    }
}
class Thread1 extends Thread
{
    private boolean isStop = false;
    public synchronized void run()
    {
        while(!isStop)
        {
            try {
                wait();
                //进入到对象的等待队列需要有notify方法去唤醒
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }
    }
    public void stopStop()
    {
        isStop = true;
    }
}

结果:

main
main
main
main
main
main
main
main
main
main
main
main
main
main
main exit
。。。还在运行

当线程中调用wait方法 t处于等待状态,此时需要interrupt()
示例:

package ThreadStop;

class ThreadStopTest {
    public static void main(String[] args)
    {
        Thread1 t = new Thread1();
        t.start();
        int index = 0;
        while(true)
        {
            if(index++ == 500)
            {
                t.stopStop();
                //终端t的线程,此时就会抛出异常
                t.interrupt();
                break;
            }
            System.out.println(Thread.currentThread().getName());
        }
        System.out.println("main exit");
    }
}
class Thread1 extends Thread
{
    private boolean isStop = false;
    public synchronized void run()
    {
        while(!isStop)
        {
            try {
                wait();
                //进入到对象的等待队列需要有notify方法去唤醒
            } catch (InterruptedException e) {
                e.printStackTrace();
                if(isStop)
                    return;
            }
            System.out.println(Thread.currentThread().getName());
        }
    }
    public void stopStop()
    {
        isStop = true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值