小编Mar*_*nik的帖子

在body其他函数中声明函数方法的用例是什么?

我在FreeRTOS源代码中遇到过这篇文章:

void vApplicationIdleHook( void )
{

    /* The simple blinky demo does not use the idle hook - the full demo does. */
    #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
    {
        extern void vFullDemoIdleHook( void );

        //* Implemented in main_full.c. */
        vFullDemoIdleHook();
    }
    #endif

}
Run Code Online (Sandbox Code Playgroud)

为什么要声明这样的函数/方法?有什么好处?我在Java中也看到了类似的代码.

c c++

6
推荐指数
1
解决办法
207
查看次数

无法在意外的地方推断出类型(可能是javac bug?)

在回答问题时,我遇到了Java类型推断的这种奇怪行为.以下工作没有问题:

new HashMap<String,Integer>().entrySet().stream()
                             .sorted(Map.Entry.comparingByValue());
Run Code Online (Sandbox Code Playgroud)

但如果我们.reversed()最后添加,它会突然失败:

new HashMap<String,Integer>().entrySet().stream()
                             .sorted(Map.Entry.comparingByValue().reversed());
Run Code Online (Sandbox Code Playgroud)

细节:在第一种情况下,comparingByValue()推断类型为Comparator<Map.Entry<String, Integer>>,但在第二种情况下,它更改为Comparator<Map.Entry<Object, V>>.

类型reversed()只是Comparator<T>对其目标类型的身份转换.为什么类型推断会在这样一个看似微不足道的步骤中失去它?我无法摆脱预感,这必定是由于推断机制中的一个错误.

作为进一步的难题,如果我们只是内联实现reversed(),那就是return Collections.reverseOrder(this);:

new HashMap<String,Integer>().entrySet().stream()
                             .sorted(reverseOrder(Map.Entry.comparingByValue());
Run Code Online (Sandbox Code Playgroud)

它又有效了.

java type-inference java-8

6
推荐指数
0
解决办法
98
查看次数

Kotlin 投影冗余

在阅读 Kotlin 泛型类型变化和投影时,我想到了一个完全陌生的概念。有人可以解释一下作者想要解释的想法是什么吗?请引用来自Kotlin in Action, MEAP 的引用:

获得out已经有out变化的类型参数的投影是没有意义的,例如List<out T>。这意味着与 相同List<T>,因为List被声明为class List<out T>。Kotlin 编译器会警告这样的投影是多余的。

这里有两个具体问题:

  1. 为什么您需要在已经输出的投影类型列表上添加投影?
  2. 即使你这样做了,你怎么得到同样的List?

java generics kotlin

6
推荐指数
1
解决办法
758
查看次数

如何将协程作为单元测试的阻塞?

我已经开始为我的MVP Android项目编写单元测试,但是我的依赖于协程的测试间歇性地失败了(通过记录和调试,我确认有时会提前进行验证delay,当然要添加此修复程序)

我已经尝试过使用,runBlocking并且Dispatchers.setMain(mainThreadSurrogate)从中发现了org.jetbrains.kotlinx:kotlinx-coroutines-test,但是到目前为止,尝试进行如此多的组合并没有取得任何成功。

abstract class CoroutinePresenter : Presenter, CoroutineScope {
    private lateinit var job: Job

    override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.Main

    override fun onCreate() {
        super.onCreate()
        job = Job()
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }
}

class MainPresenter @Inject constructor(private val getInfoUsecase: GetInfoUsecase) : CoroutinePresenter() {
    lateinit var view: View

    fun inject(view: View) {
        this.view = view
    }

    override fun onResume() {
        super.onResume()

        refreshInfo()
    }

    fun refreshInfo() …
Run Code Online (Sandbox Code Playgroud)

junit android mockito kotlin kotlinx.coroutines

6
推荐指数
1
解决办法
1225
查看次数

增强了循环性能

我和我的朋友就此发生了争执.考虑下面的片段,

for(i=0; i<someList.size(); i++) {
    //some logic
    }
Run Code Online (Sandbox Code Playgroud)

someList.size()将在每次迭代时执行,因此建议将此大小计算迁移到循环外部(之前).

现在当我像这样使用扩展for循环时会发生什么,

for(SpecialBean bean: someBean.getSpecialList()) {
//some logic
}
Run Code Online (Sandbox Code Playgroud)

是否有必要转移someBean.getSpecialList()到循环外?someBean.getSpecialList()如果我要保留第二个片段,它会执行多少次?

java performance for-loop

5
推荐指数
2
解决办法
2785
查看次数

Java内存模型是否保证了线程内写入的可见性?

考虑一个简单的单线程Java程序执行,它不涉及同步操作,只是实例变量的简单读写.简单地忽略所有写入的实现似乎符合Java内存规范.首先,§17.4适用的一般声明:

内存模型确定可以在程序中的每个点读取哪些值.隔离中每个线程的操作必须遵循该线程的语义,除了每次读取所看到的值由内存模型确定.

相关的限制因素如下(第17.4.5节):

1.在由程序顺序引起的排序之前发生:

如果x和y是同一个线程的动作,并且x在程序顺序中出现在y之前,那么hb(x,y).

2.发生 - 在一致性之前:

如果对于A中的所有读取r,其中W(r)是r看到的写入动作,那么一组动作A发生 - 在一致之前,不是hb(r,W(r))或那里的情况在A中存在写w,使得wv = rv和hb(W(r),w)和hb(w,r).

这基本上排除了它观察到的写入之前发生的读取.另一个条款只是一个完整性条款,它可以防止某些人v看到之前写的那篇内容,v然后又写了一篇同样的内容v.

我无法保证可以正确地观察到写入,只能限制对写入的内容.

我在这里错过了什么?JVM是否真的可以省略这样一个微不足道的保证?

java

5
推荐指数
1
解决办法
225
查看次数

你如何在Lucene 4.1中索引和搜索数字

在我的3.6代码中,我将数字字段添加到我的索引中,如下所示:

public void addNumericField(IndexField field, Integer value) {
        addField(field, NumericUtils.intToPrefixCoded(value));
    }
Run Code Online (Sandbox Code Playgroud)

但是现在你需要传递一个BytesRef参数,并且完全不清楚你对下一个值的意图是什么,所以我改为(正在进行中)

public void addNumericField(IndexField field, Integer value) {
        FieldType ft = new FieldType();
        ft.setStored(true);
        ft.setIndexed(true);
        ft.setNumericType(FieldType.NumericType.INT);
        doc.add(new IntField(field.getName(), value, ft));
    }
Run Code Online (Sandbox Code Playgroud)

看起来更整洁

在3.6中我还添加了覆盖queryparser以使其适用于数值范围搜索,

package org.musicbrainz.search.servlet;

import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.util.NumericUtils;
import org.musicbrainz.search.LuceneVersion;
import org.musicbrainz.search.index.LabelIndexField;
import org.musicbrainz.search.servlet.mmd1.LabelType;

public class LabelQueryParser extends MultiFieldQueryParser {

    public LabelQueryParser(java.lang.String[] strings, org.apache.lucene.analysis.Analyzer analyzer)
    {
        super(LuceneVersion.LUCENE_VERSION, strings, analyzer);
    }

     protected Query newTermQuery(Term term) {

        if(
                (term.field() == LabelIndexField.CODE.getName())
                ){ …
Run Code Online (Sandbox Code Playgroud)

java lucene search

5
推荐指数
1
解决办法
6081
查看次数

即使在日志中看到“添加事务方法”,方法也不会被事务顾问拦截

我有一个@Transactional @Controller,但它的方法正在被 Spring MVC 框架调用而没有事务。在异常跟踪中,我没有找到拦截调用的事务顾问:

org.hibernate.HibernateException: No Session found for current thread
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
org.example.businesslogic.MyController.userLoggedIn(SwiperRest.java:48)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
Run Code Online (Sandbox Code Playgroud)

另一方面,日志清楚地表明控制器方法被检测为事务性的:

DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'metaDataSourceAdvisor'
DEBUG o.s.t.a.AnnotationTransactionAttributeSource - Adding transactional method 'MyController.userLoggedIn' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG o.s.a.f.a.InfrastructureAdvisorAutoProxyCreator - Creating implicit proxy for bean 'myController' with 0 common interceptors and 1 specific interceptors
DEBUG o.s.a.f.CglibAopProxy …
Run Code Online (Sandbox Code Playgroud)

java spring transactions spring-mvc spring-jdbc

5
推荐指数
1
解决办法
2191
查看次数

请求范围的ApplicationEventListener无法接收事件

我需要为每个请求注册一个单独的应用程序事件监听器.侦听器的目的是捕获来自其他REST请求的事件,同时阻止侦听器的请求等待所有必需的事件进入.

我有这样的代码:

@Component
// @Scope(WebApplicationContext.SCOPE_REQUEST)
public static class WhistleEventListener implements ApplicationListener<WhistleEvent> {
  volatile Consumer<WhistleEvent> handler;
  @Override
  public void onApplicationEvent(WhistleEvent we) {
    final Consumer<WhistleEvent> h = handler;
    if (h != null) h.accept(we);
  }
}
@Autowired WhistleEventListener whistleEventListener;
Run Code Online (Sandbox Code Playgroud)

此代码接收事件,但是一旦取消注释@Scope注释,它就会停止接收事件.

是否支持请求范围的应用程序事件侦听器,它们应该可以工作吗?如果是这样,我可以做些什么让我的听众工作吗?

java spring spring-mvc

5
推荐指数
1
解决办法
372
查看次数

JMM 中 final 字段的语义

这里 [ http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5][1]

它说:

给定写入 w、冻结 f、动作 a(不是读取最终字段)、读取由 f 冻结的最终字段的 r1 和读取 r2 使得 hb(w, f), hb( f, a)、mc(a, r1) 和 dereferences(r1, r2),那么在确定 r2 可以看到哪些值时,我们考虑 hb(w, r2)。(这种发生在排序之前不会与其他发生在排序之前传递关闭。)

他们想在这里说什么?我知道 r2 只是读取 r1 读取的最终字段值,因此很明显 hb(w, r2),因为 r1 读取了此变量的正确版本,因为该值被 f 冻结。或者他们的意思不同?多深?此外,他们想说这个 hb 订购不会与其他 hb 订购“传递关闭”?

java concurrency multithreading

5
推荐指数
1
解决办法
304
查看次数