我刚刚开始研究Java的Executors类和newCachedThreadPool( )方法.根据API,生成的线程池会Thread为新任务重用现有对象.
我有点疑惑它是如何实现的,因为我在ThreadAPI中找不到任何允许您设置现有Thread对象行为的方法.
例如,您可以创建一个新的 Thread从一个Runnable对象,这使得Thread调用Runnable的run( )方法.但是,ThreadAPI中没有以set Runnable作为参数的setter方法.
我很感激任何指针.
当我尝试用ghc它来编译它时,抱怨函数定义左侧的参数数量是不同的.
module Example where
import Data.Maybe
from_maybe :: a -> Maybe a -> a
from_maybe a Nothing = a
from_maybe _ = Data.Maybe.fromJust
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是一个ghc限制.我试着看看我是否能找到关于Haskell 2010报告中参数数量的任何信息,但我没有成功.
这是合法的Haskell还是不是吗?如果没有,列出的参数计数限制在哪里?
我最近开始玩LoadingCache类,我得到一些意想不到的行为.我现在知道解决方案,但我发布了这个问题,以便其他人可以从中学习.
基本上,我在缓存中为某个键设置了一个非空值.关键是值在文件中的位置,并使用CacheLoader.load()方法,可以从文件中读取值.目前,LoadCache由单个线程使用.
当我运行我的应用程序时,将使用以前用于插入非空值的键的null值调用删除通知侦听器.我知道这是因为我使用assert语句检查进入缓存的所有值都不为null(这不应该发生).我也可以看到这个,因为我在CacheLoader.load()调用的方法中打印了密钥:CacheLoader.load()是唯一可以调用打印密钥的方法的方法.此外,此方法不能返回空值.
如果我做错了什么,有人可以告诉我,我会非常感激.
以下是我对加载缓存的定义.
private LoadingCache<Long,T> gcache( ) {
@SuppressWarnings( Constants.UNCHECKED )
final CacheLoader<Long,T> loader =
new CacheLoader<Long,T>() {
public T load(Long key) {
// This following method _did_ print the key for which
// the RemovalNotification listener returns a null value.
// See output further on.
return getElement( key );
}
};
@SuppressWarnings( Constants.UNCHECKED )
final RemovalListener<Long,T> listener
= new RemovalListener<Long,T>( ) {
@Override
public void onRemoval( RemovalNotification<Long,T> notification ) {
final T value = notification.getValue( …Run Code Online (Sandbox Code Playgroud)