多线程中非法启动表达式(java)

时间:2016-11-01 11:29:44

标签: java multithreading

基本上我在netbeans上运行这个代码它有效,在原子上它不适合我...任何建议,下载原子,因为我想用编译器编写java但我不想要复杂的东西,类似于括号...

问题:

public class multithreading {

    public static void execute(final String name)
    {

        for(int index=1; index<=5; index++)
        {

            System.out.println("thread " + name + " - " + index);

        }

    }


    public static void main(String []args){

       final Thread one = new Thread(() -> execute("first Thread"));
       final Thread two = new Thread(() -> execute("second Thread"));
       final Thread three = new Thread(() -> execute("third Thread"));

       one.start();
       two.start();
       three.start();

    }

}

错误:

multithreading.java:18: error: illegal start of expression
       final Thread one = new Thread(() -> execute("first Thread"));
                                      ^

multithreading.java:18: error: illegal start of expression
       final Thread one = new Thread(() -> execute("first Thread"));
                                         ^

multithreading.java:19: error: illegal start of expression
       final Thread two = new Thread(() -> execute("second Thread"));
                                      ^

multithreading.java:19: error: illegal start of expression
       final Thread two = new Thread(() -> execute("second Thread"));
                                         ^

multithreading.java:20: error: illegal start of expression
       final Thread three = new Thread(() -> execute("third Thread"));
                                        ^

multithreading.java:20: error: illegal start of expression
       final Thread three = new Thread(() -> execute("third Thread"));
                                           ^

6 errors [Finished in 0.226s]

1 个答案:

答案 0 :(得分:1)

必须是您尝试运行此版本的旧Java版本。像() -> execute("first Thread")这样的Lambda只能在Java 8之后使用。