zoukankan      html  css  js  c++  java
  • JXSE网络在后台运行的线程

    JXSE网络启动后,后台需要很多线程执行任务,下面列举典型的几个例子:

    ------------------------------------------------------------------------------------------------------------------

    使用了ReliableOutputStream,会启动一个重传任务RetransmitTask(因为要保证可靠传输)。

    其中JxtaBiDiPipe和JxtaSocket都使用了ReliableOutputStream,因此都能保证可靠传输。

    /**
     * Accepts data and packages it into messages for sending to the remote. The
     * messages are kept in a retry queue until the remote peer acknowledges
     * receipt of the message.
     
    */
    public class ReliableOutputStream extends OutputStream implements Incoming {}
    ------------------------------------------------------------------------------------------------------------------

    SRDI管理器(即Shared Resource Distributed Index) 启动周期性的推送任务SrdiManagerPeriodicPushTask

    这个任务在很多服务中都使用。例如发现服务、管道解析器(PipeResolver)等

    ------------------------------------------------------------------------------------------------------------------

    组播服务中的数据包处理器

        /**
         * Handles incoming datagram packets. This implementation uses the peer
         * group Executor service to process the datagram packets, but limits the
         * number of concurrent tasks.
         
    */
        private class DatagramProcessor implements Runnable {}

    ------------------------------------------------------------------------------------------------------------------

    总结:

    JXSE核心服务需要的线程都是通过TaskManager进行管理的,而且我们自己实现的自定义服务如果需要线程处理一些事情也要统一使用TaskManager。

    /**
     * Singleton manager for periodic, deferred and multi-threaded execution of tasks. The intention
     * of this class is to abstract away the details of how tasks will be executed, and specifics
     * of how many threads are used.
     * <p>
     * <em>NOTE</em>: This is <em>not</em> part of the stable API for JXTA, and should not be used
     * by code which is not a core module of the JXTA-JXSE implementation. We anticipate that in
     * future a similar mechanism to this will be adopted as the standard way of controlling the
     * execution thread pools in JXTA, and may be later exposed to the outside world, but the
     * details have not yet been adequately discussed.
     
    */
    public class TaskManager {}

     在这个类的构造函数中,定义了几种类型的线程池

        /**
         * Creates a task manager that uses the default or system property specified values for core pool size,
         * max pool size, idle thread timeout and scheduled pool size.
         
    */
        public TaskManager() {
            this(nullnullnullnull);
        }
        
        /**
         * Allows the construction of a task manager with explicitly specified values for each of core pool size,
         * max pool size, idle thread timeout and scheduled pool size. If null is passed for any of these parameters,
         * the system property value, if specified, will be used. Failing that, a sensible default will be applied.
         * 
         * 
    @param coreWorkerPoolSize the number of threads that will be maintained in the executor service. 
         * 
    @param maxWorkerPoolSize the maximum number of threads that will be allowed in the executor service.
         * 
    @param idleThreadTimeoutSecs the minimum amount of time that additional threads (beyond the core pool size) 
         * will stay alive before terminating.
         * 
    @param scheduledPoolSize the number of threads that will be used for the execution of deferred and periodic
         * tasks.
         
    */
        public TaskManager(Integer coreWorkerPoolSize, Integer maxWorkerPoolSize, Integer idleThreadTimeoutSecs, Integer scheduledPoolSize) {
            NamedThreadFactory NTF = new NamedThreadFactory("JxtaTaskMonitor");
                monitoringExecutor = Executors.newSingleThreadScheduledExecutor(NTF);
                int corePoolSize = getCorePoolSize(coreWorkerPoolSize);
                normalExecutor = new SharedThreadPoolExecutor(monitoringExecutor,
                                                              corePoolSize,
                                                              getMaxWorkerPoolSize(corePoolSize, maxWorkerPoolSize),
                                                              getIdleThreadTimeout(idleThreadTimeoutSecs),
                                                              TimeUnit.SECONDS,
                                                              new SynchronousQueue<Runnable>(),
                                                              new NamedThreadFactory("JxtaWorker"));
                scheduledExecutor = new SharedScheduledThreadPoolExecutor(monitoringExecutor, getScheduledPoolSize(scheduledPoolSize), new NamedThreadFactory("JxtaScheduledWorker"));
                cachedExecutor = new CachedThreadExecutorService(NTF);
                proxiedExecutors = Collections.synchronizedMap(new HashMap<String, ProxiedScheduledExecutorService>());
                started=true;
            }

    第一种是normalExecutor,以”JxtaWorker“开头(从调试结果来看有4个线程);

    第二种是scheduledExecutor,以”JxtaScheduledWorker“开头(从调试结果来看有2个线程);

    第三种是monitoringExecutor,以”JxtaTaskMonitor“开头(从调试结果来看有1个线程);

    ---------------------------------------------------------------------------------------------------------------------------------

    因此,我们通过注册各种监听器,使用回调函数接收网络数据(DiscoveryService/ResolverService/JxtaBiDiPipe/JxtaSocket/JxtaMultisocket)等等,都是在上述这些线程中执行,此时就涉及都多线程的同步问题,尤其是设计UI的时候。

    只要清楚这一点,就能较好的处理线程问题。

  • 相关阅读:
    apache 问题 You don't have permission to access /test.php on this server 解决方法
    setTimeout和setInterval实现定时器的区别
    视图Ext.Viewport和窗口Ext.Window用法
    JavaScript设置Cookie
    布局Layout
    html中select标签刷新后不回到默认值而是保持之前选择值
    设置session失效的几种方法
    面板Ext.Panel使用
    树TreePanel
    让html元素随浏览器的大小自适应垂直居中
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2184802.html
Copyright © 2011-2022 走看看