我在网上搜索了一些关于阻止I/O和非阻塞I/O的技术细节,我发现有几个人说非阻塞I/O比阻塞I/O更快.例如,在本文档中.
如果我使用阻塞I/O,那么当然阻塞的线程当然不会做任何其他事情......因为它被阻止了.但是一旦线程开始被阻塞,操作系统就可以切换到另一个线程而不会切换回来,直到阻塞的线程有事情要做.因此,只要系统上有另一个需要CPU并且没有被阻塞的线程,与基于事件的非阻塞方法相比,不应该有更多的CPU空闲时间,是吗?
除了减少CPU空闲时间之外,我还看到了另外一个选项,可以增加计算机在给定时间范围内可以执行的任务数量:减少切换线程所带来的开销.但是怎么做呢?并且开销是否足以显示可衡量的影响?以下是关于如何将其工作的想法:
它是如何工作的?如果没有,它是如何工作的?这意味着事件系统可以在不需要显式触摸堆栈的情况下工作(例如需要备份堆栈并在切换线程时将另一个线程的堆栈复制到内存中的真实调度程序)?这实际节省了多少时间?还有更多吗?
在以下代码中:
class A {
private int number;
public void a() {
number = 5;
}
public void b() {
while(number == 0) {
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果调用方法b然后启动一个触发方法a的新线程,则方法b不能保证看到更改,number
因此b
可能永远不会终止.
当然,我们可以number
volatile
解决这个问题.但是出于学术原因,我们假设这volatile
不是一个选择:
该JSR-133的常见问题告诉我们:
在我们退出synchronized块之后,我们释放了监视器,它具有将缓存刷新到主内存的效果,因此该线程所做的写操作对其他线程是可见的.在我们进入同步块之前,我们获取监视器,它具有使本地处理器高速缓存无效的效果,以便从主存储器重新加载变量.
这听起来像我只需要两个a
并b
进入和退出任何synchronized
-Block,无论他们使用什么显示器.更确切地说,它听起来像这样......:
class A {
private int number;
public void a() {
number = 5;
synchronized(new Object()) {}
}
public void b() {
while(number == 0) {
// …
Run Code Online (Sandbox Code Playgroud) 我使用ui.bootstrap.datepicker指令来显示一些日期字段.然而,大多数时候我需要相同的设置:我希望它带有一个弹出按钮和一个弹出按钮,我也想要文本的德语名称.这确实为按钮和文本以及格式反复创建了相同的代码,因此我编写了自己的指令以防止自己重复自己.
这是我的指令的一个plunkr.但是我似乎做错了.如果您使用不使用我的指令的"日期1"日期选择器选择日期选择器的日期一切正常.我期望日期2相同,但不是根据我在输入字段中提供的模板(或我预期的任何其他值)显示.toString()
日期,而是显示日期对象的表示(例如Fri Apr 03 2015 00:00:00 GMT+0200 (CEST)
).
这是我的指示:
angular.module('ui.bootstrap.demo').directive('myDatepicker', function($compile) {
var controllerName = 'dateEditCtrl';
return {
restrict: 'A',
require: '?ngModel',
scope: true,
link: function(scope, element) {
var wrapper = angular.element(
'<div class="input-group">' +
'<span class="input-group-btn">' +
'<button type="button" class="btn btn-default" ng-click="' + controllerName + '.openPopup($event)"><i class="glyphicon glyphicon-calendar"></i></button>' +
'</span>' +
'</div>');
function setAttributeIfNotExists(name, value) {
var oldValue = element.attr(name);
if (!angular.isDefined(oldValue) || oldValue === false) {
element.attr(name, value);
} …
Run Code Online (Sandbox Code Playgroud) javascript jquery angularjs angularjs-directive angular-ui-bootstrap
我是新手angular
,angular2
特别是.我正在尝试编写一个容器组件,其中应包含子组件.
例如,容器组件:
@Component({
selector: 'my-list',
template: `
<ul>
<ng-content></ng-content>
</ul>
`
})
export class MyList {
}
Run Code Online (Sandbox Code Playgroud)
子组件:
import { Component } from 'angular2/core'
@Component({
selector: 'my-item',
template: `
<li>
<ng-content></ng-content>
</li>
`
})
export class MyItem {
}
Run Code Online (Sandbox Code Playgroud)
我想做这个结构:
<my-list>
<my-item>One</my-item>
<my-item>Two</my-item>
</my-list>
Run Code Online (Sandbox Code Playgroud)
要呈现给以下一个:
<my-list>
<ul>
<li>One</li>
<li>Two</li>
</ul>
</my-list>
Run Code Online (Sandbox Code Playgroud)
但相反,我有容器的主机元素和保留的项目:
<my-list>
<ul>
<my-item>
<li>One</li>
</my-item>
<my-item>
<li>Two</li>
</my-item>
</ul>
</my-list>
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法消除宿主元素并只留下渲染模板?
我有2个课程延伸WebSecurityConfigurerAdapter
.并不能让他们一起工作.
这个想法如下:
WebSecurityConfigurerAdapter
只添加自定义过滤器到安全链.过滤器执行一些自定义身份验证并保存Authentication
到SecurityContext
.这通常很好.配置如下(导入省略): @Order(1)
@Configuration
@EnableWebMvcSecurity
public class BestSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private BestPreAuthenticationFilter ssoAuthenticationFilter;
@Bean
protected FilterRegistrationBean getSSOAuthenticationFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(ssoAuthenticationFilter);
// Avoid include to the default chain
filterRegistrationBean.setEnabled(false);
return filterRegistrationBean;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(ssoAuthenticationFilter, SecurityContextPersistenceFilter.class);
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Autowired
private BestAuthenticationProvider authenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
} …
Run Code Online (Sandbox Code Playgroud) 在maven项目中,项目的版本包含在<version>
pom.xml文件的attritbute中.在git flow模型中创建新版本时,我需要修改版本号.本文解释了如何完成此操作(没有maven):
另外它说:
正是在发布分支的开始,即将发布的版本被分配了一个版本号 - 而不是之前的版本号.直到那一刻,开发分支反映了"下一个版本"的变化,但不清楚"下一个版本"最终是否会变为0.3或1.0,直到发布分支开始.该决定是在发布分支的开始时做出的,并由项目关于版本号冲突的规则执行.
我在这里看到与maven结合的两个问题:
1.1-SNAPSHOT
.现在我们已经将其更改为仅1.1
在发布分支上并将其合并到master.精细.但是我们也应该将该分支合并回来进行开发,为此我们需要将版本调整为例如1.2-SNAPSHOT
.也许我们不应该在发布分支上做到这一点,因为该提交不应该是发布的一部分.实际上,我们可能应该在分支开发之后立即进行此更改,因为所有未来的开发提交都将针对下一个版本.当谷歌搜索问题时,我发现了一些关于maven-plugins的文章可以自动化这个过程,这可能很有意思,但这个问题实际上是关于git图应该是什么样子以及版本提交应该在哪里而不是我怎么做使用maven-plugin自动执行此操作.
我有一个组件A,它在模板中使用组件B,c,D:
###template-compA.html
<comp-b></comp-b>
<comp-c [myinput]="obj.myinput"></comp-c>
<comp-d ></comp-d>
Run Code Online (Sandbox Code Playgroud)
...等等
为简化起见,假设它们只是组件A中的一个指令:
###template-compA.html
<comp-b></comp-b>
Run Code Online (Sandbox Code Playgroud)
我的comp-b有自己的依赖项(服务或其他comp).
如果我想以这种方式测试comp-a:
TestBed.configureTestingModule({
declarations: [comp-A],
imports: [ReactiveFormsModule],
}).overrideComponent(FAQListComponent, {
set: {
providers: [
{ provide: comp-AService, useValue: comp-AListSVC }
]
}
})
.compileComponents();
Run Code Online (Sandbox Code Playgroud)
它不会正常工作.所以我这样做:
TestBed.configureTestingModule({
declarations: [comp-A, comp-B],
imports: [ReactiveFormsModule],
}).overrideComponent(FAQListComponent, {
set: {
providers: [
{ provide: comp-AService, useValue: comp-AListSVC }
]
}
})
.compileComponents();
Run Code Online (Sandbox Code Playgroud)
它也不起作用,因为comp-b没有自己的依赖项.在这里我很困惑,如果我必须每次导入和重新安装所有其他组件,我怎么能进行单元测试?它看起来像是一项非常大的工作.还有另外一种方法吗?使用具有自己的依赖关系的嵌套组件测试组件的最佳实践是什么?
非常感谢,
斯特凡.
Java 8中的默认"paralellStream()"使用公共ForkJoinPool
,如果在提交任务时公共池线程用尽,则可能是延迟问题.但是,在许多情况下,有足够的CPU功率可用且任务足够短,因此这不是问题.如果我们确实有一些长期运行的任务,这当然需要仔细考虑,但对于这个问题,让我们假设这不是问题.
但是ForkJoinPool
,即使有足够的CPU功率,填充实际上不执行任何CPU绑定工作的I/O任务也是一种引入瓶颈的方法.我明白了.然而,这就是我们所拥有的ManagedBlocker
.因此,如果我们有一个I/O任务,我们应该只允许ForkJoinPool
在一个内部管理它ManagedBlocker
.听起来非常简单.但令我惊讶的ManagedBlocker
是,使用一个相当复杂的API来实现简单的事情.毕竟我认为这是一个常见的问题.所以我只是构建了一个简单的实用方法,使其ManagedBlocker
易于用于常见情况:
public class BlockingTasks {
public static<T> T callInManagedBlock(final Supplier<T> supplier) {
final SupplierManagedBlock<T> managedBlock = new SupplierManagedBlock<>(supplier);
try {
ForkJoinPool.managedBlock(managedBlock);
} catch (InterruptedException e) {
throw new Error(e);
}
return managedBlock.getResult();
}
private static class SupplierManagedBlock<T> implements ForkJoinPool.ManagedBlocker {
private final Supplier<T> supplier;
private T result;
private boolean done = false;
private SupplierManagedBlock(final Supplier<T> supplier) {
this.supplier = supplier;
}
@Override …
Run Code Online (Sandbox Code Playgroud) 假设我有以下超级简单的组件:
class MyComponent {
@Input()
simpleContent: string;
@ContentChild(TemplateRef)
content: TemplateRef<any>;
}
Run Code Online (Sandbox Code Playgroud)
<div>
<div *ngIf="simpleContent; else complexContent">{{simpleContent}}</div>
<ng-template #complexContent>
<ng-container [ngTemplateOutlet]="content"></ng-container>
</ng-template>
</div>
Run Code Online (Sandbox Code Playgroud)
根据用户的选择,它可以像这样使用:
<my-component simpleContent="Hello world!"></my-component>
Run Code Online (Sandbox Code Playgroud)
或者它可以像这样使用:
<my-component>
<ng-template>Hello world!</ng-template>
</my-component>
Run Code Online (Sandbox Code Playgroud)
虽然第二个选项允许嵌入 HTML 并因此更复杂的格式,但用户可以选择简单的方法,在大多数情况下只使用一个属性。
我想简化组件并去掉模板中的 if/else。我想象这样的事情:
class MyComponent {
@ContentChild(TemplateRef)
content: TemplateRef<any>;
@Input()
set simpleContent(value: string) {
this.content = new TemplateRef(value);
}
}
Run Code Online (Sandbox Code Playgroud)
<div>
<ng-container [ngTemplateOutlet]="content"></ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)
显然这行不通,因为 new TemplateRef(value)
是不可能的。
有没有办法在运行时动态创建一个基本的简单模板?
我在PostgreSQL中创建了一个这样的表:
create table myTable (
dateAdded timestamp(0) without time zone null default (current_timestamp at time zone 'UTC');
)
Run Code Online (Sandbox Code Playgroud)
我选择"没有时区",因为我知道我的应用程序使用的所有时间戳都是 UTC.据我所知,"带时间戳"的唯一区别是我可以在其他时区提供值,然后转换为UTC.但是,我想避免这种自动转换,因为如果我知道我的值是UTC,它们几乎没有任何好处.
当我在测试表中添加新记录并使用pgAdmin查看表的内容时,我可以看到插入日期已经以UTC格式正确保存.
但是,当我尝试使用JDBC选择值时,该值减去2小时.我位于UTC + 2,因此看起来JDBC假定表中的日期不是UTC时间戳,而是UTC + 2时间戳而是尝试转换为UTC.
一些谷歌搜索显示,JDBC标准规定了与当前时区的转换,但是可以通过向getTimestamp/setTimestamp调用提供Calander来防止这种情况.但是提供日历并没有任何差别.这是我的MyBatis/Jodatime转换器:
@MappedTypes(DateTime.class)
public class DateTimeTypeHandler extends BaseTypeHandler<DateTime> {
private static final Calendar UTC_CALENDAR = Calendar.getInstance(DateTimeZone.UTC.toTimeZone());
@Override
public void setNonNullParameter(PreparedStatement ps, int i,
DateTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, new Timestamp(parameter.getMillis()), UTC_CALENDAR);
}
@Override
public DateTime getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return fromSQLTimestamp(rs.getTimestamp(columnName, UTC_CALENDAR));
}
/* further get methods with …
Run Code Online (Sandbox Code Playgroud) angular ×3
java ×3
angularjs ×1
blocking ×1
datetime ×1
git ×1
git-flow ×1
io ×1
jasmine ×1
java-stream ×1
javascript ×1
jdbc ×1
jquery ×1
maven ×1
nonblocking ×1
postgresql ×1
spring-boot ×1
unit-testing ×1
versioning ×1