首页 \ 问答 \ Adobe中的FuntionalInterface注释类型(FuntionalInterface Annotation Type in Java)

Adobe中的FuntionalInterface注释类型(FuntionalInterface Annotation Type in Java)

我正在阅读关于java注释的内容,并出现了一个新的疑问。

在文档中解释了FunctionalInterface Annotation Type:

具有一个抽象方法声明的接口称为功能接口。编译器验证所有使用@FunctionalInterface注释的接口,接口实际上只包含一个抽象方法。 如果使用此批注注释的接口不是功能接口,则会生成编译时错误。 在类,注释类型和枚举上使用此注释也是一个编译时错误。 FunctionalInterface注释类型是标记接口。

我做了一些测试,我不需要用这个注释类型标记我的界面。 然后,我的问题是:如果每个具有一个方法的接口始终是一个功能接口,为什么我需要这个注释?

示例代码

// @FunctionalInterface
interface Wizard {
    int spell(String power);
}

class TestLambda {
    public static void main(String[] args) {
        Wizard gandalf = str -> str.length();
        int power = gandalf.spell(args[0]);
        System.out.println("The spell length is: " + power+ " points");
    }
}

I was reading about java annotation and a new doubt appears.

In the documentation explain the FunctionalInterface Annotation Type:

An interface with one abstract method declaration is known as a functional interface.The compiler verifies all interfaces annotated with a @FunctionalInterface that the interfaces really contain one and only one abstract method. A compile-time error is generated if the interfaces annotated with this annotation are not functional interfaces. It is also a compile-time error to use this annotation on classes, annotation types, and enums. The FunctionalInterface annotation type is a marker interface.

I did some test and I did not need to mark my interface with this annotation type. Then, my question is: Why do I need this annotation if every interface with one method is always a functional interface?

Exampe Code

// @FunctionalInterface
interface Wizard {
    int spell(String power);
}

class TestLambda {
    public static void main(String[] args) {
        Wizard gandalf = str -> str.length();
        int power = gandalf.spell(args[0]);
        System.out.println("The spell length is: " + power+ " points");
    }
}

原文:https://stackoverflow.com/questions/29954781
更新时间:2024-03-26 13:03

最满意答案

i被它的参考所捕获。 将您的代码更改为

for (int i = 0; i < N; i++)
{
    var thInx = i;
    ThreadList.Add(new Thread(() => Worker(thInx)));
    ThreadList[thInx].Start();
}

欲了解更多信息: http//csharpindepth.com/articles/chapter5/closures.aspx


i is captured by its reference. Change your code as

for (int i = 0; i < N; i++)
{
    var thInx = i;
    ThreadList.Add(new Thread(() => Worker(thInx)));
    ThreadList[thInx].Start();
}

For more info: http://csharpindepth.com/articles/chapter5/closures.aspx

相关问答

更多
  • 第一种方法很简单。 如果您希望负载在螺纹上均匀平衡,这也是足够的。 在某些情况下,特别是如果bin_index的复杂性非常依赖于参数值,其中一个线程最终可能会比其他任务重要得多。 记住:当最后的线程结束时,任务完成。 第二种方法稍微复杂一些,但如果任务足够精细(任务数量比线程数量大得多),则平衡负载更均匀。 请注意,将计算放在单独的线程中可能会有问题。 当多个线程同时执行时,确保bin_index正常工作。 谨防为中间结果使用全局或静态变量。 此外,“直方图[bin_index(i1,i2,i3,i4)] ...
  • 只需将通过外循环的次数乘以内循环的总和即可 total = outer *(inner1 + inner2) var count = 0; for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { count++; } for (var k = 0; k < 10; k++) { count++; } } 这是一个jsfiddle http://jsfiddle.net/th ...
  • 你可以使用STEP : for x = 0 to 100 step 10 next x 这将带你通过0, 10, 20... 100 既然你想从1开始,去1, 10, 20... 100 ,这里稍作修改 for x = 0 to 100 step 10 if x = 0 then y = 1 else y = x end if '// use y in all calculations downstream instead of x ...
  • 如果你想要两个条件都是真的 j<5,k>-1 使用&&运算符,而不是逗号 j<5 && k>-1 在逗号分隔的列表中,逗号仅生成最后一个值,因此在您的代码中,您实际上只使用第二个条件k>-1 。 If you want both conditions to be true j<5,k>-1 use the && operator, not the comma j<5 && k>-1 In a comma separated list, the comma yields only the last v ...
  • 必须显式设置嵌套并行性,因为在大多数实现中默认情况下禁用它。 站在OpenMP 4.0标准中,您必须设置OMP_NESTED环境变量: OMP_NESTED环境变量通过设置nest-var ICV的初始值来控制嵌套并行性。 此环境变量的值必须为true或false。 如果环境变量设置为true,则启用嵌套并行性; 如果设置为false,则禁用嵌套并行性。 如果OMP_NESTED的值既不是true也不是false,则程序的行为是实现定义的。 以下行应该适用于bash: export OMP_NESTED= ...
  • 根本原因是您没有正确使用发送和接收缓冲区。 sendbuff和receivebuff是指针,所以直接使用它们而不是它们的地址。 MPI_Send(&sendbuff, nGenes, MPI_DOUBLE, cc, 0, MPI_COMM_WORLD); MPI_Recv(&receivebuff, nGenes, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); 必须更换 MPI_Send(sendbuff, nGenes, MPI_DOUBLE, ...
  • 我没有很多使用Python的经验,但更多的是一般情况:如果循环中的迭代彼此独立,则只能使用多个线程。 这意味着迭代内的计算不应该取决于前一个的结果。 如果迭代是独立的,您可以通过为每个工作线程提供作业的“一部分”来拆分工作。 这可以通过为每个线程提供相同的循环,但具有不同的启动和停止条件来实现。 因此,使用多个线程将导致更快地完成循环(多少取决于您的处理器)。 这个主题可能会有所帮助: 如何在Python中使用线程? I don't have a lot of experience using Python ...
  • 在Matlab中,无论你对for循环中的循环索引变量( a )做什么都会被抛弃,并且在下一遍开始时会重置a 。 所以循环内的a = a + 1没有效果。 请参阅MATLAB中是否存在foreach? 如果是这样,如果基础数据发生变化,它的表现如何? 。 In Matlab, whatever you do to the loop index variable (a) inside a for loop is thrown away, and a gets reset at the beginning of ...
  • i被它的参考所捕获。 将您的代码更改为 for (int i = 0; i < N; i++) { var thInx = i; ThreadList.Add(new Thread(() => Worker(thInx))); ThreadList[thInx].Start(); } 欲了解更多信息: http : //csharpindepth.com/articles/chapter5/closures.aspx i is captured by its reference. ...
  • 好吧,你有点过分复杂了。 尝试这个: void new_value(...){ #pragma omp parallel for num_threads(4) for(int i = 0; i < N; i++) arr[i] = update(); } int main(int argc, char** argv){ for(int i = 0; i < MAX_VALUE; i++) new_value(...); return 0; } We ...

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果