观察者模式管理多个线程

如果有多个线程在执行,其中的某一个线程死掉了怎么办呢?如上一篇所说,可以通过线程池的形式重启该线程。本次不在使用线程池,而是通过JDK自带的观察者来实现:如果有线程死掉,重新启动该线程。

观察者模式的思想是定义观察者和被观察者,使用观察者来管理被观察者。

观察者:

import java.util.Observable;
import java.util.Observer;

/**
 *  创建观察者
 */
public class ThreadListener implements Observer{
    @Override
    public void update(Observable o, Object arg) {
        ThreadTask threadTask = (ThreadTask)o;
        String customerId = threadTask.customerId;

        //将线程重新加入到监听中
        threadTask.addObserver(this);

        System.out.println(customerId+"线程重启...");
        new Thread(threadTask,customerId).start();

    }
}

被观察者:

被观察者都是一个个的线程,所以也就是线程继承Observable

import java.util.Observable;

/**
 * 将线程作为被观察者
 */
public class ThreadTask extends Observable implements Runnable{

    public String customerId;
    private int count = 0;
    public ThreadTask(){

    }
    public ThreadTask(String customerId){
        this.customerId = customerId;
    }

    public void run() {
        while (true)
            try {
                String threadName = Thread.currentThread().getName();
                System.out.println(threadName+":正在运行,count:"+count);
                Thread.sleep(3000);

                //对其中的线程1做异常处理
                if((count == 5 || count == 10) && "门店1".equals(threadName)){
                    count++;
                    throw new RuntimeException("我是自定义的异常信息");
                }
                count++;
            }catch (Exception e){
                System.out.println("##抛出异常...");
                doBusiness();
                break;
            }
    }

    public void doBusiness(){
        if(true){
            super.setChanged();
        }
        //通知观察者调用update()方法,重启线程
        notifyObservers();
    }
}

加入观察者

public class ThreadTest {

    public static void main(String[] args) {

        ThreadListener threadListener = new ThreadListener();

        ThreadTask t1 = new ThreadTask("门店1");
        ThreadTask t2 = new ThreadTask("门店2");

        t1.addObserver(threadListener);
        t2.addObserver(threadListener);

        //将门店名做为线程名称,方便后面追踪
        Thread thread1 = new Thread(t1,t1.customerId);
        Thread thread2 = new Thread(t2,t2.customerId);

        thread1.start();
        thread2.start();

    }
}

执行结果:

门店2:正在运行,count:0
门店1:正在运行,count:0
门店2:正在运行,count:1
门店1:正在运行,count:1
门店2:正在运行,count:2
门店1:正在运行,count:2
门店2:正在运行,count:3
门店1:正在运行,count:3
门店2:正在运行,count:4
门店1:正在运行,count:4
门店2:正在运行,count:5
门店1:正在运行,count:5
门店2:正在运行,count:6
##抛出异常...
门店1线程重启...
门店1:正在运行,count:6
门店2:正在运行,count:7
门店1:正在运行,count:7
门店2:正在运行,count:8
门店1:正在运行,count:8
门店2:正在运行,count:9
门店1:正在运行,count:9
门店2:正在运行,count:10
门店1:正在运行,count:10
门店2:正在运行,count:11
##抛出异常...
门店1线程重启...
门店1:正在运行,count:11
门店2:正在运行,count:12
门店1:正在运行,count:12

然后我们发现在线程挂掉的时候,通过观察者新起了一个线程来执行该任务。

这里主要是notifyObservers(),通知观察者。然后观察者可以通过update方法获取该任务的具体信息。然后执行接下来的操作。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值