Selector定义

Channel接口定义:[url]http://donald-draper.iteye.com/blog/2369111[/url]
AbstractInterruptibleChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369238[/url]
SelectableChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369317[/url]
SelectionKey定义:[url]http://donald-draper.iteye.com/blog/2369499[/url]
SelectorProvider定义:[url]http://donald-draper.iteye.com/blog/2369615[/url]
AbstractSelectableChannel定义:[url]http://donald-draper.iteye.com/blog/2369742[/url]
NetworkChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369773[/url]
ServerSocketChannel定义:[url]http://donald-draper.iteye.com/blog/2369836[/url]
本身打算讲ServerSocketChannel的具体实现的,看了ServerSocketChannelImp的方法中设计到选择器,所以我们先看一下选择器,再看ServerSocketChannelImp:
package java.nio.channels;

import java.io.Closeable;
import java.io.IOException;
import java.nio.channels.spi.SelectorProvider;
import java.util.Set;


/**
* A multiplexor of {@link SelectableChannel} objects.
*Selector是可选择通道的多路复用器
* A selector may be created by invoking the {@link #open open} method of
* this class, which will use the system's default {@link
* java.nio.channels.spi.SelectorProvider </code>selector provider<code>} to
* create a new selector. A selector may also be created by invoking the
* {@link java.nio.channels.spi.SelectorProvider#openSelector openSelector}
* method of a custom selector provider. A selector remains open until it is
* closed via its {@link #close close} method.
*选择器通过系统默认的选择器open实现创建,或者SelectorProvider的#openSelector方法。
在选择器没关闭之前,都处于打开状态
* <a name="ks">
*
* A selectable channel's registration with a selector is represented by a
* {@link SelectionKey} object. A selector maintains three sets of selection
* keys:
*可选择通道注册到选择器的token,我们用SelectionKey表示。选择器主要维护3个选择key集合。
* [list]
*
* <li><p> The <i>key set</i> contains the keys representing the current
* channel registrations of this selector. This set is returned by the
* {@link #keys() keys} method.
</li>
*key set集合表示当前注册到选择的通道对应的选择key。这个集合通过keys方法返回。
* <li> The <i>selected-key set</i> is the set of keys such that each
* key's channel was detected to be ready for at least one of the operations
* identified in the key's interest set during a prior selection operation.
* This set is returned by the {@link #selectedKeys() selectedKeys} method.
* The selected-key set is always a subset of the key set.
</li>
*selected-key set集合表示在一个选择操作后,注册到选择器的通道已经准备就绪的
选择key集合。通过#selectedKeys方法返回,selected-key set集合总是key set的子集。
* <li> The <i>cancelled-key</i> set is the set of keys that have been
* cancelled but whose channels have not yet been deregistered. This set is
* not directly accessible. The cancelled-key set is always a subset of the
* key set.
</li>
*cancelled-key集合,表示通道还没有反注册,但选择key已经取消的选择key。这个集合不能直接访问。
cancelled-key集合总是key set的子集。
* [/list]
*
* All three sets are empty in a newly-created selector.
*在选择器创建时,三个集合都为空。
* <p> A key is added to a selector's key set as a side effect of registering a
* channel via the channel's {@link SelectableChannel#register(Selector,int)
* register} method. Cancelled keys are removed from the key set during
* selection operations. The key set itself is not directly modifiable.
*key set只会通过选择通道的注册方法添加选择key到key集合中。已取消的key在选择操作中被移除。
key set集合自己直接修改。
* <p> A key is added to its selector's cancelled-key set when it is cancelled,
* whether by closing its channel or by invoking its {@link SelectionKey#cancel
* cancel} method. Cancelling a key will cause its channel to be deregistered
* during the next selection operation, at which time the key will removed from
* all of the selector's key sets.
*无论是由于通道关闭,还是选择key取消,key被取消后,将会添加到选择器的取消key集合。
取消一个key将会使通道在下一次选择操作中,从选择器反注册,同时取消key将会从选择器的key集合中移除。
* <a name="sks"><p> Keys are added to the selected-key set by selection
* operations. A key may be removed directly from the selected-key set by
* invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
* method or by invoking the {@link java.util.Iterator#remove() remove} method
* of an {@link java.util.Iterator </code>iterator<code>} obtained from the
* set. Keys are never removed from the selected-key set in any other way;
* they are not, in particular, removed as a side effect of selection
* operations. Keys may not be added directly to the selected-key set.

*选择操作将会把已经就绪的通道对应的选择key添加到 selected-key set 集合中。
通过set的remove和迭代器的remove操作,将会从取消key集合中,移除选择key。
除选择操作可以从key集合中移除key,其他的方式或方法不能从选择key中移除key。
key不能直接添加的可选择key集合。
*
* <a name="selop">
* <h4>Selection</h4>
*
* During each selection operation, keys may be added to and removed from a
* selector's selected-key set and may be removed from its key and
* cancelled-key sets. Selection is performed by the {@link #select()}, {@link
* #select(long)}, and {@link #selectNow()} methods, and involves three steps:
*

*在每一个选择操作过程中,可能从可选择key集合中添加或者移除可以,可能从key集合或者key取消集合中
移除一个key。选择器执行#select,select(long)和#selectNow方法,将会涉及3步:
* [list=1]
*
* <li> Each key in the cancelled-key set is removed from each key set of
* which it is a member, and its channel is deregistered. This step leaves
* the cancelled-key set empty.
</li>
* 1.将会清空取消key集合,与取消key相关的通道将会反注册。这一步取消key集合将为空。
* <li> The underlying operating system is queried for an update as to the
* readiness of each remaining channel to perform any of the operations
* identified by its key's interest set as of the moment that the selection
* operation began. For a channel that is ready for at least one such
* operation, one of the following two actions is performed:

*2.在每一次选择操作开始,底层操作系统都将检查和更显通道已经准备就绪的操作事件。
在通道准备好至少一个的操作事件下,将会执行以下两个动作:
* <ol type=a>
*
* <li> If the channel's key is not already in the selected-key set then
* it is added to that set and its ready-operation set is modified to
* identify exactly those operations for which the channel is now reported
* to be ready. Any readiness information previously recorded in the ready
* set is discarded.
</li>
*2.1 如果通道相关的选择key不在可选择key集合中,则添加key到可选择key集合,并更新就绪的
操作事件,先前的就绪事件集将会被遗弃。
* <li> Otherwise the channel's key is already in the selected-key set,
* so its ready-operation set is modified to identify any new operations
* for which the channel is reported to be ready. Any readiness
* information previously recorded in the ready set is preserved; in other
* words, the ready set returned by the underlying system is
* bitwise-disjoined into the key's current ready set.
</li>
*2.2 如果通道相关key已经在可选择key集合中,更新就绪操作集,并保留先前准备就绪的操作事件,
换句话,底层操作系统的返回的bitwise-disjoined就绪集将会更新到当前就绪操作事件集。
* [/list]</li>
*
* If all of the keys in the key set at the start of this step have empty
* interest sets then neither the selected-key set nor any of the keys'
* ready-operation sets will be updated.
*3.如果在这一步开始时,key集合中的所有key的兴趣操作事件为空,可选择key集合或其他任何key的
就绪操作事件集没有更新(这一步不太明白,想表示什么意思?,理解的网友给我留言,共同成长。
这个要结合具体的选择方法的实现,这个在以后的文章中,再看)
* <li> If any keys were added to the cancelled-key set while step (2) was
* in progress then they are processed as in step (1).
</li>
*在第二步的过程中,key被添加到取消key集合,将会返回第一步移除取消key
* </ol>
*
* Whether or not a selection operation blocks to wait for one or more
* channels to become ready, and if so for how long, is the only essential
* difference between the three selection methods.

*无论一个选择操作在阻塞等待一个或多个通道准备就绪,到目前为止等待多久,
三个选方法基本上不同。
*
* <h4>Concurrency</h4>
*并发性
* Selectors are themselves safe for use by multiple concurrent threads;
* their key sets, however, are not.
*选择器是线程安全的,而他的几个key集合不是线程安全的
* <p> The selection operations synchronize on the selector itself, on the key
* set, and on the selected-key set, in that order. They also synchronize on
* the cancelled-key set during steps (1) and (3) above.
*选择操作同步选择器,key集合,可选择key集合,同时在第一步和第三步中同步取消key集合。
* <p> Changes made to the interest sets of a selector's keys while a
* selection operation is in progress have no effect upon that operation; they
* will be seen by the next selection operation.
*当一个选择key的兴趣操作事件集在选择操作的过程中改变时,对当前选择操作没有影响,
当下一次选择操作中,才能被看到。
* <p> Keys may be cancelled and channels may be closed at any time. Hence the
* presence of a key in one or more of a selector's key sets does not imply
* that the key is valid or that its channel is open. Application code should
* be careful to synchronize and check these conditions as necessary if there
* is any possibility that another thread will cancel a key or close a channel.
*在任何时候,通道都有可能关闭,key都有可能取消。一个key存在与选择器的key集合中,
不意味着key有效或key关联的通道打开。如果其他线程有可能取消key或关闭通道,
应用必须小心这些同步,并检查需要的条件。
* <p> A thread blocked in one of the {@link #select()} or {@link
* #select(long)} methods may be interrupted by some other thread in one of
* three ways:
*一个线程阻塞在选择操作中,可以被其他线程以一下三种方式中断:
* [list]
*
* <li><p> By invoking the selector's {@link #wakeup wakeup} method,
*
</li>
*选择器调用#wakeup方法
* <li> By invoking the selector's {@link #close close} method, or
*
</li>
*选择器调用#close方法
* <li> By invoking the blocked thread's {@link
* java.lang.Thread#interrupt() interrupt} method, in which case its
* interrupt status will be set and the selector's {@link #wakeup wakeup}
* method will be invoked.
</li>
*调用阻塞线程的中断方法Thread#interrupt,线程中断位被设置,选择器的#wakeup方法将会
被调用。
* [/list]
*
* The {@link #close close} method synchronizes on the selector and all
* three key sets in the same order as in a selection operation.
*关闭方法将会同步到选择操作中的选择器和关联的3个key集合。
* <a name="ksc">
*
* <p> A selector's key and selected-key sets are not, in general, safe for use
* by multiple concurrent threads. If such a thread might modify one of these
* sets directly then access should be controlled by synchronizing on the set
* itself. The iterators returned by these sets' {@link
* java.util.Set#iterator() iterator} methods are <i>fail-fast:</i> If the set
* is modified after the iterator is created, in any way except by invoking the
* iterator's own {@link java.util.Iterator#remove() remove} method, then a
* {@link java.util.ConcurrentModificationException} will be thrown.

* key集合和可选择key集合,一般情况下多线程访问时不是线程安全的。如果线程想要直接修改
相关集合应该控制集合同步。在迭代器创建时,如果集合被修改,Set#iterator将会fail-fast。
任何调用迭代器的Iterator#remove方法,都将抛出ConcurrentModificationException。
*
* @author Mark Reinhold
* @author JSR-51 Expert Group
* @since 1.4
*
* @see SelectableChannel
* @see SelectionKey
*/

public abstract class Selector implements Closeable {

/**
* Initializes a new instance of this class.
*/
protected Selector() { }

/**
* Opens a selector.
*用系统默认的SelectorProvider实例打开一个选择器
* The new selector is created by invoking the {@link
* java.nio.channels.spi.SelectorProvider#openSelector openSelector} method
* of the system-wide default {@link
* java.nio.channels.spi.SelectorProvider} object.

*
* @return A new selector
*
* @throws IOException
* If an I/O error occurs
*/
public static Selector open() throws IOException {
return SelectorProvider.provider().openSelector();
}

/**
* Tells whether or not this selector is open.
*判断选择器是否打开
* @return <tt>true</tt> if, and only if, this selector is open
*/
public abstract boolean isOpen();

/**
* Returns the provider that created this channel. </p>
*返回创建选择器的选取器服务提供者。
* @return The provider that created this channel
*/
public abstract SelectorProvider provider();

/**
* Returns this selector's key set.
*返回选择器的选择key集合
* The key set is not directly modifiable. A key is removed only after
* it has been cancelled and its channel has been deregistered. Any
* attempt to modify the key set will cause an {@link
* UnsupportedOperationException} to be thrown.
*选择key集合不能直接地修改。在通道反注册或选择key取消时,相关的选择key将会
从选择key集合中移除。任何尝试修改选择key集合的操作,将抛出UnsupportedOperationException
* <p> The key set is [url=#ksc]not thread-safe[/url].

*
* @return This selector's key set
*
* @throws ClosedSelectorException 通道已关闭,则抛出ClosedSelectorException
* If this selector is closed
*/
public abstract Set<SelectionKey> keys();

/**
* Returns this selector's selected-key set.
*返回选择器的已选择的key集合,及操作事件已经就绪的key集合
* Keys may be removed from, but not directly added to, the
* selected-key set. Any attempt to add an object to the key set will
* cause an {@link UnsupportedOperationException} to be thrown.
*选择key不可以直接添加到已选择的key集合,但可以移除。
任何尝试添加选择key到key集合的操作,将抛出UnsupportedOperationException
* <p> The selected-key set is [url=#ksc]not thread-safe[/url].

*
* @return This selector's selected-key set
*
* @throws ClosedSelectorException
* If this selector is closed
*/
public abstract Set<SelectionKey> selectedKeys();

/**
* Selects a set of keys whose corresponding channels are ready for I/O
* operations.
*选择一个关联通道已经准备好IO操作的选择key数量。
* This method performs a non-blocking <a href="#selop">selection
* operation</a>. If no channels have become selectable since the previous
* selection operation then this method immediately returns zero.
*这个方法以非阻塞模式执行。由于先前已经进行选择操作,没有通道可选择,此方法,
将立即返回为0.
* <p> Invoking this method clears the effect of any previous invocations
* of the {@link #wakeup wakeup} method.

*调用此方法,将清除wakeup方法的效果。
* @return The number of keys, possibly zero, whose ready-operation sets
* were updated by the selection operation
*
* @throws IOException
* If an I/O error occurs
*
* @throws ClosedSelectorException
* If this selector is closed
*/
public abstract int selectNow() throws IOException;

/**
* Selects a set of keys whose corresponding channels are ready for I/O
* operations.
*选择一个关联通道已经准备好IO操作的选择key数量。
* This method performs a blocking <a href="#selop">selection
* operation</a>. It returns only after at least one channel is selected,
* this selector's {@link #wakeup wakeup} method is invoked, the current
* thread is interrupted, or the given timeout period expires, whichever
* comes first.
*这个方法以非阻塞模式执行。在至少有一个通道可选择,选择器wakeup方法被调用,
当前线程被中断,或超时,无论这几种情况,那一个先发生,都将直接返回。
* <p> This method does not offer real-time guarantees: It schedules the
* timeout as if by invoking the {@link Object#wait(long)} method.

*此方法不能保证事实的保证,因为通过Object#wait(long)去调度超时时间。
* @param timeout If positive, block for up to <tt>timeout</tt>
* milliseconds, more or less, while waiting for a
* channel to become ready; if zero, block indefinitely;
* must not be negative
*
* @return The number of keys, possibly zero,
* whose ready-operation sets were updated
*
* @throws IOException
* If an I/O error occurs
*
* @throws ClosedSelectorException
* If this selector is closed
*
* @throws IllegalArgumentException
* If the value of the timeout argument is negative
*/
public abstract int select(long timeout)
throws IOException;

/**
* Selects a set of keys whose corresponding channels are ready for I/O
* operations.
*选择一个关联通道已经准备好IO操作的选择key数量。
* This method performs a blocking <a href="#selop">selection
* operation</a>. It returns only after at least one channel is selected,
* this selector's {@link #wakeup wakeup} method is invoked, or the current
* thread is interrupted, whichever comes first.

*这个方法以阻塞模式执行。在至少有一个通道可选择,选择器wakeup方法被调用,
当前线程被中断,无论这几种情况,那一个先发生,都将直接返回。
* @return The number of keys, possibly zero,
* whose ready-operation sets were updated
*
* @throws IOException
* If an I/O error occurs
*
* @throws ClosedSelectorException
* If this selector is closed
*/
public abstract int select() throws IOException;

/**
* Causes the first selection operation that has not yet returned to return
* immediately.
*唤醒一个等待选取key可用的线程。
* If another thread is currently blocked in an invocation of the
* {@link #select()} or {@link #select(long)} methods then that invocation
* will return immediately. If no selection operation is currently in
* progress then the next invocation of one of these methods will return
* immediately unless the {@link #selectNow()} method is invoked in the
* meantime. In any case the value returned by that invocation may be
* non-zero. Subsequent invocations of the {@link #select()} or {@link
* #select(long)} methods will block as usual unless this method is invoked
* again in the meantime.
*如果其他线程当前正在阻塞或超时等待选择操作,wakeup方法调用,将唤醒上述线程。
如果当前没有选择操作在进程中,下一个调用选择操作相关方法将立即返回,除非#selectNow
方法在同时调用,#selectNow是非阻塞的。在wakeup之后,任何调用select操作的都将返回非0。
#select和select(long)将会向平常一样阻塞,除非wakeup方法同时被调用。
* <p> Invoking this method more than once between two successive selection
* operations has the same effect as invoking it just once.

*在多次选择操作中,wakeup只起效一次。
* @return This selector
*/
public abstract Selector wakeup();

/**
* Closes this selector.
*关闭选择器
* If a thread is currently blocked in one of this selector's selection
* methods then it is interrupted as if by invoking the selector's {@link
* #wakeup wakeup} method.
*如果当前线程阻塞在选择器的选择操作上,调用选择器的wakeup方法,将会消除中断位。
* <p> Any uncancelled keys still associated with this selector are
* invalidated, their channels are deregistered, and any other resources
* associated with this selector are released.
*所有与选择器相关联的通道的选择key将会无效,通道将被反注册,与选择器关联的
任何资源都将会被释放
* <p> If this selector is already closed then invoking this method has no
* effect.
*如果选择器已关闭,再次调用无效
* <p> After a selector is closed, any further attempt to use it, except by
* invoking this method or the {@link #wakeup wakeup} method, will cause a
* {@link ClosedSelectorException} to be thrown.

*在选择器关闭后,任何尝试wakeup方法调用的,将抛出ClosedSelectorException
* @throws IOException
* If an I/O error occurs
*/
public abstract void close() throws IOException;

}
Python自动化监控:实时捕捉LOF基金溢价套利机会并微信预警 本文详细介绍了如何使用Python构建一个自动化监控系统,实时捕捉LOF基金的溢价套利机会。通过AKShare获取基金数据,结合雪球API计算实时溢价率,并利用企业微信API实现微信推送预警,帮助投资者高效、自动地发现并抓住转瞬即逝的套利机会。 阅读详情

相关推荐

别再手动配IP了!用NI-USRP Configuration Utility快速搞定USRP 2954与LabVIEW连接

本文详细介绍了如何使用NI-USRP Configuration Utility快速配置USRP 2954与LabVIEW的连接,解决传统手动IP配置的繁琐问题。通过图形化工具自动完成设备发现、IP设置和连接验证,大幅提升软件无线电实验效率,特别适合信号收发应用场景。

weixin_27102543的博客 292

Java NIO - Selector

Java NIO - Selector Selector能够检测多个注册的通道上是否有事件发生(多个channel以事件的方式可以注册到同一个selector)。如果有事件发生,便获取事件,然后对每个事件进行相应的处理。这样就可以只用一个单线程去管理多个通道,也就是管理多个连接和请求 Selector类 Selector类是一个抽象类,说明如下: public abstract class Selector implements Closeable 一些方法: 1.public static Select

了凡 632

手把手教你用STM32F429+FreeRTOS搭建开源SIP电话(附代码与避坑指南)

本文详细介绍了如何使用STM32F429和FreeRTOS搭建开源SIP电话系统,涵盖硬件选型、FreeRTOS任务划分、PJSIP协议栈移植、音频系统优化及安全实践。通过实战案例和代码示例,帮助开发者快速实现嵌入式VoIP通信,特别适合物联网和音视频通信领域的应用开发。

weixin_33582311的博客 356

【NIO】解读 java.nio.channels.Selector

目录 Part 1. What's Selector? Part 2.Why Selector? Part 3. How to use Selector? 1) Create Selector 2) Channel's Register 3) Selection Key 4) Selector.select() 5) Selector.selectedKeys() Part 4.Demo Train Part 1. What's Selector? Selector is the ..

暂七师师长常乃超 1440

从《视觉SLAM十四讲》到项目实战:一文读懂拓扑地图与语义地图为何是服务机器人的未来

本文探讨了服务机器人地图技术的演进,从传统几何地图到拓扑地图与语义地图的跨越。通过分析栅格地图的局限性和拓扑地图、语义地图的优势,揭示了它们在动态开放环境中的智能应用。文章还介绍了多模态语义融合架构和动态语义更新技术,为服务机器人的未来发展提供了技术路线图。

weixin_30292843的博客 478

AutoSAR MemMap模块详解及使用注意事项

在本文中,我们将深入探讨MemMap模块的实例解析,并介绍在使用该模块时需要注意的一些事项。在上述代码中,我们使用了MEMMAP_START_SEC_VAR_32BIT和MEMMAP_START_SEC_CODE来定义MemMap区域的起始位置,使用MEMMAP_STOP_SEC_VAR_32BIT和MEMMAP_STOP_SEC_CODE来定义MemMap区域的结束位置。1.合理划分MemMap区域:根据实际需求,合理划分MemMap区域,将不同类型的变量放置在不同的区域中,以便于管理和访问。

weixin_50547796的博客 2532

java nio Selector源码分析

目录 简介 构造函数,方法open,isOpen,provider 方法keys,selectedKeys,selectNow,2个select 方法wakeup,close 简介 package java.nio.channels; import java.io.Closeable; import java.io.IOException; import java.nio.channels.spi.SelectorProvider; import java.util.Set; /**

今天吃了吗的博客 346

74.9008深度刷机原理|高通QPST/Firehose底层刷写协议详解

移动设备刷机与底层维修技术指南 本文为移动设备维修人员和高级玩家提供全面的刷机与底层维修技术指南,涵盖高通、联发科和苹果A系列芯片的主要操作。 主要内容 核心操作:Bootloader解锁、Fastboot刷写、EDL深度修复、iTunes DFU模式恢复和分区表重建 适用场景:系统故障修复、底层损坏恢复、安全限制解除、系统降级和深度硬件维修 关键技术:详细解析刷机协议栈层级、关键分区作用和刷机安全机制 操作步骤: 环境搭建与驱动安装 Bootloader解锁(以小米为例) 高通机型系统刷写 苹果设备DFU

2601_95651806的博客 205

android 定义selector,Android selector选择器自定义属性

Android selector选择器自定义属性Android系统为UI的背景制定了灵活的绘制方案,drawable方式。开发者可以通过各种各样的drawable样式来为自己的UI灵活展示各自的风格。在selector选择器中,系统提供了类似state_enables的属性,让组件在不同的状态下展示不同的UI效果。在笔者研(shi)究(yong)timessquare项目时,发现selector的...

weixin_36462387的博客 960

【深度学习新浪潮】如何上手DINO v3:Meta新一代视觉大模型实战指南

自定义数据集类(适配本地/公开数据集)# 去除batch维度# 构建数据加载器# 定义分类模型(冻结预训练层,仅训练分类头)self.classifier = nn.Linear(dino_model.config.hidden_size, num_classes) # 分类头global_feat = outputs.global_attention_pool # 取全局特征# 初始化模型# 冻结预训练层(仅训练分类头)# 定义损失函数和优化器。

专注:太空算力、AI infra、异构调度、大模型成本工程 491

【毕设级教程】从0到1实现基于YOLOv8的xView卫星图像目标检测系统(含UI界面,助力遥感智能毕设)

本文基于YOLOv8模型设计了一个面向xView卫星图像的目标检测系统。首先对xView数据集进行了格式转换处理,将原始多边形标注转换为YOLO格式的边界框标注。随后通过配置YOLOv8模型参数完成训练,实现了对卫星图像中建筑、车辆等多类目标的准确识别。为提高系统交互性,采用PyQt开发了可视化界面,支持图像选择、实时检测和结果展示功能。整个系统兼顾学术价值和实用价值,为遥感领域的自动化目标检测提供了可行方案。

m0_64661770的博客 130

自定义View selector无效

一个自定义的View 继承自 RelativeLayout 重写了 onTouchEvent()后 使用 selector 无效

u013258802的博客 1519

【华为OD机试真题E卷】300、 出租车计费 、靠谱的车 | 机试真题+思路参考+代码分析(C++、Java、Py、C语言、JS)

题目描述 >程序员小明打了一辆出租车去上班,出于职业敏感,他注意到这辆出租车的计费表有点问题,总是偏大,出租车司机解释说他不喜欢数字4,所以改装了计费表,任何数字位置遇到数字4就直接跳过,其余功能都正常 >  比如: 1、23再多一块钱就变为25; 2、39再多一块钱变为50; 3、399再多一块钱变为500; >  小明识破了司机的伎俩,准备利用自己的学识打败司机的阴谋,给出计费表的表面读数,返回实际产生的费用

KJ.JK 4万+

【Android】【UI】在XML中定义Selector

天天向上 Up Up Up (*^▽^*) 651

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

selector自定义查询

selector自定义查询

大诚子的博客 345

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Android使用selector自定义按钮

以前定义按钮的时候,使用

u011782528的专栏 1009

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

在langchain中使用自定义example selector

在之前的文章中,我们提到了可以在跟大模型交互的时候,给大模型提供一些具体的例子内容,方便大模型从这些内容中获取想要的答案。这种方便的机制在langchain中叫做FewShotPromptTemplate。如果例子内容少的话,其实无所谓,我们可以把所有的例子都发送给大语言模型进行处理。但是如果例子太多的话,每次都发送如此多的内容,会让我们的钱包承受不住。毕竟那些第三方的大语言模型是按token收费的。怎么办呢?能不能找到一个经济又有效的方法来完成我们的工作呢?

程序那些事 1499

Android自定义矩形及selector、shape的使用

【声明】转载请注明出处,此文出自指尖飞落的博客:http://blog.youkuaiyun.com/huntersnail ——每天写一篇博客,每天做一点技术积累! Android自定义矩形及selector、shape的使用 由于项目开发需要,以前虽然用过selector、shape但是都没有好好去研究过,只知道用,不知道它们的属性具体有哪些作用。虽然网上一查就都知道了,感觉还是

指尖飞落的博客 7500

自定义RadioButton Selector

问题 需要实现自定义的RadioButton selector实现步骤1 . 增加selector <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/b2_p

liken的专栏 4505

安卓selector和自定义shape

一、安卓Selector背景选择器 1.在drawable中配置Android的selector。   android:drawable="@drawable/pic2" />   android:drawable="@drawable/pic3" /> 2.android:listSelector或者android:backgr

DearShare的博客 487

android中selector的用法与自定义颜色值

参考:http://blog.youkuaiyun.com/wenwen091100304/article/details/49667293         selector中文的意思选择器,在Android中常常用来作组件的背景,这样做的好处是省去了用代码控制实现组件在不同状态下不同的背景颜色或图片的变换。使用十分方便。         selector的定义:         s

shenlong_no1的博客 2557

android selector 自定义 drawable state

selector 的运作原理 这一篇已经讲的很清楚了: http://blog.youkuaiyun.com/guolin_blog/article/details/17045157 最后一步显示是通过DrawableContainer的selectDrawable(int idx) 然后mCurrDrawable.setVisible(......) 来实现的图像变化。 目前项目中实现夜间模

fyfcauc的专栏 1855

自定义Button 直接设置selector

Custom button that can implement click effect don't need create a selector.xml How To Use 0. create res/values/attrs.xml add code name="SelectorButton"> name="normal_drawable" format=

Hanks软件库 1134

无需自定义View,彻底解放shape,selector吧

【最新版本已支持selector,详情查看 https://github.com/JavaNoober/BackgroundLibrary】 前言 作为一个android程序员,对于shape、selector这两个标签一定不陌生。每当UI设计师给我们设计出一个个button背景的时候,我们...

chongyun5569的博客 85

Flume内部原理——Interceptor & Selector和IDEA编写自定义拦截器案例

目录 一、Flume内部原理 二、Interceptor和Selector配置文件案例 三、自定义拦截器案例(IDEA) 一、Flume内部原理 Flume内部从Source -&gt; Channel -&gt; Sink,中间会先后经历拦截器和选择器; (1)Event:一个数据单元,也是Flume数据传输的基本单元,事务类型;          包括一个可选的消息头head...

攻城狮Kevin 866
上一篇: FileDescriptor定义
下一篇: AbstractSelector定义
Donald_Draper
博客等级 码龄14年 9粉丝 106原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值