[英]Identifier expected after this token Error
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() 行中,我收到相同的错误 -> 令牌启动时的语法错误,此令牌后应有标识符。
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.作为替代方案,您可以在初始化程序或方法中移动
start()
调用语句。
Here is a example with an initializer :这是一个带有初始化程序的示例:
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();
}
}
Put it inside the main method.把它放在 main 方法中。 It will work properly if you put it inside
如果你把它放在里面它会正常工作
public static void main(String args[]){
//your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.