我正在模拟一个银行系统,我有100,000个交易要运行.每种类型的事务都实现了runnable,我可以发生各种类型的事务.
transactions 是一个Runnables数组.
理想情况下,以下代码可以解决我的问题:
for (Transaction transaction : transactions) {
new Thread(transaction).start();
}
Run Code Online (Sandbox Code Playgroud)
但是,java.lang.OutOfMemoryError: unable to create new native thread在尝试启动100,000个线程时,显然会发生一个问题.
接下来我尝试实现一个ExecutorService来创建一个线程池来管理我的100,000个runnables.
ExecutorService service;
int cpus = Runtime.getRuntime().availableProcessors();
// cpus == 8 in my case
service = Executors.newFixedThreadPool(cpus);
for (Transaction transaction : transactions) {
service.execute(transaction);
}
Run Code Online (Sandbox Code Playgroud)
尝试这种方法时,长进程会"占用"JVM.例如,一种类型的事务需要30-60秒才能执行.在分析应用程序时,在长事务发生时不允许其他线程运行.

在这种情况下,线程6在其处理完成之前不允许任何其他线程运行.
所以我的问题是:如何在不遇到内存问题的情况下尽快运行100,000个事务?如果ExecutorService是答案,那么如何阻止非常长的事务占用JVM并允许其他事务同时运行?
编辑:
我故意强制某些类型的事务发生30-60秒,以确保我的线程程序正常工作.每个事务锁定一个帐户,有10个帐户.这是我的方法,它占用了JVM :(被称为run())
public void makeTransaction() {
synchronized(account) {
long timeStarted = System.nanoTime();
long timeToEnd = timeStarted + nanos;
this.view = new BatchView(transactionNumber, account.getId());
this.displayView();
while(true) {
if(System.nanoTime() …Run Code Online (Sandbox Code Playgroud) Lumen的文档指出:“ Lumen开箱即用地支持多个广播驱动程序:Pusher,Redis和log用于本地开发和调试的驱动程序。每个驱动程序均包含一个配置示例。BROADCAST_DRIVER配置选项可用于设置默认驱动程序。”
在我的.env文件中,我已设置BROADCAST_DRIVER=pusher。我在哪里/如何配置我的推送程序ID,密钥和机密?我看到在Laravel中有一个用于设置这些选项的配置文件config/broadcasting.php。我在流明的哪里可以设置这些选项?
暂时,我已经编辑Illuminate\Broadcasting\BroadcastManager并硬编码了我的值。
protected function createPusherDriver(array $config)
{
// override
$app_id = 'hidden';
$key = 'hidden';
$secret = 'hidden';
return new PusherBroadcaster(
new Pusher($key, $secret, $app_id, Arr::get($config, 'options', []))
);
}
Run Code Online (Sandbox Code Playgroud) 我正在努力在我的集成测试中与我的 google 地方自动完成结果进行交互。
var placeSelector = '.pac-container .pac-item:first-child';
exports.runTest = function(test) {
casper.waitForSelector('input.street-address'); // wait for page to load
casper.sendKeys('input.street-address', 'fake address here', {keepFocus: true});
casper.waitUntilVisible(placeSelector);
casper.then(function() {
casper.click(placeSelector); // THIS DOES NOT DO ANYTHING
// if its possible to trigger the event in the context of the page, I
// could probably do so. However, I've scoured google's docs and cannot find the
// event that is fired when a place is clicked upon.
casper.evaluate(function() {
//google.maps.places.Autocomplete.event.trigger(???);
});
}); …Run Code Online (Sandbox Code Playgroud) javascript integration-testing google-maps google-places-api casperjs
casperjs ×1
concurrency ×1
google-maps ×1
java ×1
javascript ×1
jvm ×1
laravel ×1
lumen ×1
php ×1
pusher ×1