我想异步调用一个与主线程分开的函数.我是Java并发的新手,所以我问这样做的最佳方法是什么:
for(File myFile : files){
MyFileService.resize(myfile) <--- this should be async
}
Run Code Online (Sandbox Code Playgroud)
while循环继续,而函数MyFileService.resize在后台运行,我的每个文件都在集合中.
我听说Java8的CompletionStage可能是很好的方法.什么是最好的方法?
我正在尝试向用户显示具有预定义值的芯片列表,并让用户选择其中的一些。我的目标是通过点击选择多个筹码,并根据反应做出反应。
问题是,没有发出单个事件。
我做错了什么?
manage-roles.component.html
<mat-chip-list [selectable]="true" [multiple]="true"
(change)="onChange($event)">
<mat-chip *ngFor="let role of possibleRoles"
[selectable]="true" (selectionChange)="onChipSelect($event)">
{{role}}
</mat-chip>
</mat-chip-list>
Run Code Online (Sandbox Code Playgroud)
manage-roles.component.html
@Component({
selector: 'hr-manage-roles',
templateUrl: './manage-roles.component.html'
})
export class ManageRolesComponent implements AfterViewInit {
@ViewChild(MatChipList) chipList: MatChipList;
possibleRoles: string[] = Roles; // some const
ngAfterViewInit(): void {
this.chipList.chipSelectionChanges.subscribe(change => {
console.log(change); // not fires
})
}
onChange(change: MatChipListChange){
console.log(change); // not fires
}
onChipSelect(change: MatChipSelectionChange) {
console.log(change); // not fires
}
}
Run Code Online (Sandbox Code Playgroud)
包.json
"dependencies": {
"@angular/animations": "^5.0.0",
"@angular/cdk": "^5.0.0",
"@angular/common": "^5.1.0",
"@angular/compiler": "^5.1.0", …Run Code Online (Sandbox Code Playgroud) 这是我在某个服务类中的方法。它是公开的,因此应该进行测试。我只是不知道我应该测试什么。我会模拟Writer和 spyOn 函数调用,但是使用这种实现是不可能的(不是吗?)
我正在使用Mockito和JUnit
现在,我只能使函数抛出和断言该异常
有什么帮助吗?
@Override
public void initIndexFile(File emptyIndexFile) {
try {
Writer writer = new FileWriter(emptyIndexFile);
writer.write("[]");
writer.close();
} catch (IOException e) {
throw new IndexFileInitializationException(
"Error initialization index file " + emptyIndexFile.getPath()
);
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道为什么这两个Date对象在控制台中有不同的输出.在我看来它应该是相同的,但我可能是错的:)
var twoLinesSetup = new Date();
twoLinesSetup.setHours(0, 0, 0);
var inlineSetup = new Date().setHours(0, 0, 0)
console.log('twoLinesSetup', twoLinesSetup);
console.log('inlineSetup', inlineSetup);Run Code Online (Sandbox Code Playgroud)
和控制台
twoLinesSetup:2017年5月8日星期一00:00:00 GMT + 0200
inlineSetup:1494194400521
为什么会这样?
java ×2
angular ×1
asynchronous ×1
concurrency ×1
date ×1
javascript ×1
mockito ×1
testing ×1
unit-testing ×1
writer ×1