zoukankan      html  css  js  c++  java
  • java-线程观察整个生命周期

    package com.srdsoft.test;

    //被观察的对象,定义一个虚拟类,没有实现Runnable run方法,所以用 abstract
    public abstract class ObservableRunnable implements Runnable {

        final protected LifeCycleListener listener;

        public ObservableRunnable(final LifeCycleListener listener) {
            this.listener = listener;
        }

        protected void notifyChange(final RunnableEvent event) {
            listener.onEvent(event);
        }

        public enum RunnableState {
            RUNNING, ERROR, DONE
        }

    //中间传递事件和数据的对象.
        public static class RunnableEvent {
            private final RunnableState state;
            private final Thread thread;
            private final Throwable cause;

            public RunnableEvent(RunnableState state, Thread thread, Throwable cause) {
                this.state = state;
                this.thread = thread;
                this.cause = cause;
            }

            public RunnableState getState() {
                return state;
            }

            public Thread getThread() {
                return thread;
            }

            public Throwable getCause() {
                return cause;
            }
        }
    }


    package com.srdsoft.test;

    //观察者接口
    public interface LifeCycleListener {
        
        void onEvent(ObservableRunnable.RunnableEvent event);
    }

    package com.srdsoft.test;

    import java.util.List;

    //观察者
    public class ThreadLifeCycleObserver implements LifeCycleListener {

        private final Object LOCK = new Object();

        public void concurrentQuery(List《String》 ids) {
            if (ids == null || ids.isEmpty())
                return;

            ids.stream().forEach(id -》 new Thread(new ObservableRunnable(this) {
                @Override
                public void run() {
                    try {
                        notifyChange(new RunnableEvent(RunnableState.RUNNING, Thread.currentThread(), null));
                        System.out.println("query for the id " + id);
                        Thread.sleep(1000L);
                        notifyChange(new RunnableEvent(RunnableState.DONE, Thread.currentThread(), null));
                    } catch (Exception e) {
                        notifyChange(new RunnableEvent(RunnableState.ERROR, Thread.currentThread(), e));
                    }
                }
            }, id).start());
        }

        @Override
        public void onEvent(ObservableRunnable.RunnableEvent event) {
            synchronized (LOCK) {
                System.out.println("The runnable [" + event.getThread().getName() + "] data changed and state is [" + event.getState() + "]");
                if (event.getCause() != null) {
                    System.out.println("The runnable [" + event.getThread().getName() + "] process failed.");
                    event.getCause().printStackTrace();
                }
            }
        }
    }

    package com.srdsoft.test;

    import java.util.Arrays;


    public class ThreadLifeCycleTest {
        public static void main(String[] args) {
            new ThreadLifeCycleObserver().concurrentQuery(Arrays.asList("1", "2"));
        }
    }
  • 相关阅读:
    cinder支持nfs快照
    浏览器输入URL到返回页面的全过程
    按需制作最小的本地yum源
    创建可执行bin安装文件
    RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in major version and less than or equal in minor version as the specified version cap 4.11.
    惠普IPMI登陆不上
    Linux进程状态——top,ps中看到进程状态D,S,Z的含义
    openstack-neutron基本的网络类型以及分析
    openstack octavia的实现与分析(二)原理,架构与基本流程
    flask上下文流程图
  • 原文地址:https://www.cnblogs.com/luckForever/p/7254097.html
Copyright © 2011-2022 走看看