无法解析方法';start()';在Java中实现了可运行的
我想交替运行两个线程来写下字母表。我不知道我在代码中写错了什么,但是IDE无法解析.start()-方法。搜索了很多,但找不到我问题的答案。我对每一个想法都心存感激无法解析方法';start()';在Java中实现了可运行的,java,Java,我想交替运行两个线程来写下字母表。我不知道我在代码中写错了什么,但是IDE无法解析.start()-方法。搜索了很多,但找不到我问题的答案。我对每一个想法都心存感激 public class ABCThread_2 implements Runnable { private boolean issmall; private boolean istall; public ABCThread_2(boolean istall, boolean issmall) {
public class ABCThread_2 implements Runnable
{
private boolean issmall;
private boolean istall;
public ABCThread_2(boolean istall, boolean issmall)
{
this.istall = istall;
this.issmall = issmall;
}
public void run()
{
if(issmall)
{
for (char c = 'a'; c <= 'z'; c++)
{
try
{
Thread.sleep(250);
}
catch (InterruptedException e)
{
}
System.out.print(c);
}
}
else if(istall)
{
for (char c = 'A'; c <= 'Z'; c++)
{
try
{
Thread.sleep(250);
}
catch(InterruptedException e)
{
}
System.out.print(c);
}
}
}
public static void main(String [] args)
{
ABCThread_2 th1 = new ABCThread_2(false, true);
ABCThread_2 th2 = new ABCThread_2(true, false);
th1.start();
th2.start();
}
}
public类ABCThread_2实现可运行
{
私人小商店;
私有布尔istall;
公共ABCThread_2(布尔istall、布尔issmall)
{
this.istall=istall;
this.issmall=issmall;
}
公开募捐
{
如果(issmall)
{
对于(char c='a';cRunnable
没有start
方法(该ABCThread_2
将继承)。您肯定想调用Thread.start
。在这种情况下,使用Runnable
创建线程实例:
public static void main(String[] args) {
Thread th1 = new Thread(new ABCThread_2(false, true));
Thread th2 = new Thread(new ABCThread_2(true, false));
th1.start();
th2.start();
}
Runnable
没有start
方法
您混淆了Runnable
和Thread
s。Thread
s接受Runnable
,并在新线程中调用它
您需要显式地创建一个新的线程
:
// Create your Runnable
Runnable runnable = new ABCThread_2(false, true);
// Then give it to a new instance of a Thread to run
Thread th1 = new Thread(runnable);
// And now you can start the Thread
th1.start();
虽然您在这里的命名会让事情变得混乱。ABCThread_2
确实应该被重命名为描述性的,并且不表明它本身是Thread
Hi-DeGtz的子类。您似乎对Java不熟悉,Thread和Runnable之间的混淆相当普遍。但是您还没有完成您的家庭作业。我建议在使用stackoverflow之前,先搜索“java线程教程”并阅读类似的内容(),以便找到非常容易找到的答案。