似乎不可能创建一个缓存的线程池,它可以创建的线程数限制.
以下是在标准Java库中实现静态Executors.newCachedThreadPool的方法:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
Run Code Online (Sandbox Code Playgroud)
因此,使用该模板继续创建固定大小的缓存线程池:
new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new SynchronusQueue<Runable>());
Run Code Online (Sandbox Code Playgroud)
现在,如果你使用它并提交3个任务,一切都会好的.提交任何进一步的任务将导致被拒绝的执行异常.
试试这个:
new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runable>());
Run Code Online (Sandbox Code Playgroud)
将导致所有线程按顺序执行.即,线程池永远不会有多个线程来处理您的任务.
这是ThreadPoolExecutor的execute方法中的错误?或者这可能是故意的?还是有其他方式?
编辑:我想要一些与缓存线程池完全相同的东西(它根据需要创建线程,然后在一些超时后杀死它们)但是它可以创建的线程数量受到限制,并且一旦有了它就能够继续排队其他任务达到了它的线程限制.根据sjlee的回应,这是不可能的.查看ThreadPoolExecutor的execute()方法确实是不可能的.我需要继承ThreadPoolExecutor并覆盖execute(),就像SwingWorker一样,但SwingWorker在其execute()中所做的是一个完整的hack.
java concurrency multithreading executorservice threadpoolexecutor
我目前正在努力从Cucumber中删除我的Sinatra应用程序的帮助方法.
我有一个简单的会话身份验证(通过cookie)的Sinatra应用程序,我想通过剔除logged_in?我的Cucumber方案的帮助方法来转向身份验证.Sinatra和Cucumber似乎有关于会话的问题,所以我想到只使用Mocha来解决这个问题.
但是我不知道如何Sinatra::Application从Given-Block中访问该实例来固定方法.
假设我每秒要求大约100个请求,每个请求应该在1到3秒之间(在一个完美的世界中).
我会创建一个300连接的池吗?或稍微高一些,以弥补潜在的尖峰?
我有一个叫做的帮助器Zend_View_Helper_FormVars,我的一个模块使用过它.我也有一个共同的帮手application/common/helpers/GeneralFunctions.php
我试图调用一个函数从Zend_View_Helper_FormVars该公司在 GeneralFunctions.php.
这是以下的简短版本Zend_View_Helper_FormVars:
class Zend_View_Helper_FormVars
{
public $reqFieldVisual='<span class="req">*</span>';
public $roles=array('admin'=>'admin', 'user'=>'user');
public $paymentMethods=array('1'=>'Check', '2'=>'Credit Card',
'3'=>'Cash', '4'=>'Other');
public function formVars(){
$this->baseUrl=Zend_Controller_Front::getInstance()->getBaseUrl();
return $this;
}
public function mkCategoryCodeSelectGroup($codeTypeArr=array(),
$codesArr=array()) {
$html='';
$html.=Zend_View_Helper_GeneralFunctions::generalFunctions()->progressMeter();
return $html;
}
}
Run Code Online (Sandbox Code Playgroud)
这是以下代码GeneralFunctions.php:
class Zend_View_Helper_GeneralFunctions
{
public function generalFunctions(){
$this->baseUrl=Zend_Controller_Front::getInstance()->getBaseUrl();
return $this;
}
public function progressMeter() {
$html='';
$html.='<span id="progressWrapper">';
$html.='<span id="progressMeter"></span>';
$html.='</span>';
$html.='';
return $html;
}
}
Run Code Online (Sandbox Code Playgroud)
另外,忘了提一下我GeneralFunctions在这样的Bootstrap中自动加载了帮助器,它已经可用于我的所有模块了:
$view->addHelperPath(APPLICATION_PATH .'/common/helpers', 'View_Helper');
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的,但是收到错误: …
我阅读了Stackoverflow.com上关于启动开源项目的问题.我遵循所有步骤,但问题仍然没有答案.
谢谢,
嗨,我想在文件中搜索与此类似的内容:
Start Cycle
report 1
report 2
report 3
report 4
End Cycle
Run Code Online (Sandbox Code Playgroud)
....继续......
我想搜索"Start Cycle",然后从中提取报告1并报告3.我的正则表达式看起来像这样
(Start Cycle .*\n)(.*\n)(.*\n)(.*\n)
Run Code Online (Sandbox Code Playgroud)
上面的正则表达式选择开始循环和接下来的三行..但我想省略我的结果中的第三行.那可能吗?或者任何更简单的perl脚本都可以完成?我期待一个结果,如:
Start Cycle
report 1
report 3
Run Code Online (Sandbox Code Playgroud) 有没有办法在Flex DataGrid中选择单个单元格,然后选择单元格内的文本,还是选择整个单元格进行复制+粘贴?它甚至不需要复制成excel友好格式,平面文本也没关系.到目前为止,我只能选择整行,而且似乎并没有很好地复制.我正在使用DataGrid来显示错误日志,我希望能够将堆栈跟踪信息复制出来并将其丢入错误报告或电子邮件中.
谢谢.
我写了一些类似于以下内容的代码:
String SKIP_FIRST = "foo";
String SKIP_SECOND = "foo/bar";
int skipFooBarIndex(String[] list){
int index;
if (list.length >= (index = 1) && list[0].equals(SKIP_FIRST) ||
list.length >= (index = 2) &&
(list[0] + "/" + list[1]).equals(SKIP_SECOND)){
return index;
}
return 0;
}
String[] myArray = "foo/bar/apples/peaches/cherries".split("/");
print(skipFooBarIndex(myArray);
Run Code Online (Sandbox Code Playgroud)
这通过分配索引来更改if语句内部的状态.但是,我的同事非常不喜欢这个.
这是一种有害的做法吗?有什么理由这样做吗?
我在许多项目中使用了一组控制器和视图.我想知道我是否可以将其置于类库中并将其重用为普通的lib文件.我怎样才能做到这一点?
我很喜欢STL.它使编码算法非常方便,因为它为您提供了所有原语,如parition,find,binary_search,iterators,priority_queue等.另外,您根本不必担心内存泄漏.
我唯一关心的是运算符重载的性能损失,这是使STL工作所必需的.为了比较,我认为它依赖于==提供所需的语义.如果我们将类添加到容器中,我们需要重载==运算符.
为方便起见,我失去了多少效率?
关于内存泄漏的另一个问题:
java ×3
apache-flex ×1
asp.net-mvc ×1
c++ ×1
concurrency ×1
copy-paste ×1
cucumber ×1
database ×1
datagrid ×1
if-statement ×1
memory-leaks ×1
mocha.js ×1
open-source ×1
perl ×1
php ×1
regex ×1
ruby ×1
sinatra ×1
state ×1
stl ×1
zend-view ×1