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
最满意答案
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 asfor (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)] ...
-
如何在另一个for循环中找到2个for循环的迭代次数?(How to find the number of iterations of 2 for-loops within another for-loop?)[2024-02-07]
只需将通过外循环的次数乘以内循环的总和即可 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 ... -
循环中的迭代(Iterations In From Loop)[2024-04-06]
你可以使用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 ... -
for循环超过其限制(for loop exceeding its limit)[2022-07-13]
如果你想要两个条件都是真的 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 ... -
使用一个线程和一个包含多个线程的for循环执行一个部分(Doing a section with one thread and a for-loop with multiple threads)[2022-08-28]
必须显式设置嵌套并行性,因为在大多数实现中默认情况下禁用它。 站在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 ...
-
循环中的迭代次数(Number of iterations in a loop)[2022-01-19]
在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 ... -
for循环生成的工作线程列表超出了循环迭代次数(List of worker threads spawned by for loop is exceeding for-loop iterations)[2023-10-04]
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 ...