此令牌错误后预期的标识符

时间:2022-04-05 22:28:25
Thread thread1 = new Thread() {
    public void run() {

    }
};

Thread thread2 = new Thread() {
    public void run() {

    }
};

thread1.start();
thread2.start();

How can I get rid out of this error? In the line thread1.start() and thread2.start() I get the same error -> Syntax Error on token start, Identifier expected after this token.

我怎样才能摆脱这个错误?在thread1.start()和thread2.start()行中,我得到了相同的错误 - >令牌启动时的语法错误,此令牌后预期的标识符。

1 个解决方案

#1


2  

Syntax Error on token start, Identifier expected after this token.

令牌启动时出现语法错误,此令牌后预期的标识符。

means that you declared these statements:

意味着你声明了这些陈述:

thread1.start();
thread2.start();

as members of the class.
But these are not valid member declarations.

作为班上的成员。但这些都不是有效的成员声明。

These don't create any issue as these are valid declarations :

这些不会产生任何问题,因为它们是有效的声明:

Thread thread1 = new Thread() {
    public void run() {

    }
};

Thread thread2 = new Thread() {
    public void run() {

    }
};

As alternative, you could move the start() invocation statements in an initializer or a method.
Here is a example with an initializer :

或者,您可以在初始化程序或方法中移动start()调用语句。以下是初始化程序的示例:

public class Foo {

    Thread thread1 = new Thread() {
        public void run() {

        }
    };

    Thread thread2 = new Thread() {
        public void run() {

        }
    };

    {
      thread1.start();
      thread2.start();
    }

}

Or if it makes more sense, you can also change the fields into local variables and declare the whole statements in a method :

或者如果它更有意义,您还可以将字段更改为局部变量并在方法中声明整个语句:

public class Foo {

    public void myMethod(){    

       Thread thread1 = new Thread() {
          public void run() {

          }
       };

       Thread thread2 = new Thread() {
         public void run() {

        }
       };

       thread1.start();
       thread2.start();
    }

}

#1


2  

Syntax Error on token start, Identifier expected after this token.

令牌启动时出现语法错误,此令牌后预期的标识符。

means that you declared these statements:

意味着你声明了这些陈述:

thread1.start();
thread2.start();

as members of the class.
But these are not valid member declarations.

作为班上的成员。但这些都不是有效的成员声明。

These don't create any issue as these are valid declarations :

这些不会产生任何问题,因为它们是有效的声明:

Thread thread1 = new Thread() {
    public void run() {

    }
};

Thread thread2 = new Thread() {
    public void run() {

    }
};

As alternative, you could move the start() invocation statements in an initializer or a method.
Here is a example with an initializer :

或者,您可以在初始化程序或方法中移动start()调用语句。以下是初始化程序的示例:

public class Foo {

    Thread thread1 = new Thread() {
        public void run() {

        }
    };

    Thread thread2 = new Thread() {
        public void run() {

        }
    };

    {
      thread1.start();
      thread2.start();
    }

}

Or if it makes more sense, you can also change the fields into local variables and declare the whole statements in a method :

或者如果它更有意义,您还可以将字段更改为局部变量并在方法中声明整个语句:

public class Foo {

    public void myMethod(){    

       Thread thread1 = new Thread() {
          public void run() {

          }
       };

       Thread thread2 = new Thread() {
         public void run() {

        }
       };

       thread1.start();
       thread2.start();
    }

}