2011-12-14 53 views
1

我正在学习Java中的线程启动教程。 代码是非常基本的Java线程中的语法错误

public interface Runnable { 

void run(); 

} 

public class RunnableThread implements Runnable { 

    Thread runner; 
    public RunnableThread() { 
    } 
    public RunnableThread(String threadName) { 
     runner = new Thread(this, threadName); // (1) Create a new thread. 
     System.out.println(runner.getName()); 
     runner.start(); // (2) Start the thread. 
    } 
    public void run() { 
     //Display info about this particular thread 
     System.out.println(Thread.currentThread()); 
    } 
} 

但我得到一个解析错误在这行 亚军=新主题(这一点,threadName);

no suitable constructor found for Thread(RunnableThread,java.lang.String) 
constructor java.lang.Thread.Thread(java.lang.ThreadGroup,java.lang.Runnable,java.lang.String,long) is not applicable 
    (actual and formal argument lists differ in length) 
constructor java.lang.Thread.Thread(java.lang.ThreadGroup,java.lang.Runnable,java.lang.String) is not applicable 
    (actual and formal argument lists differ in length) 
constructor java.lang.Thread.Thread(java.lang.Runnable,java.lang.String) is not applicable 
    (actual argument RunnableThread cannot be converted to java.lang.Runnable by method invocation conversion) 
constructor java.lang.Thread.Thread(java.lang.ThreadGroup,java.lang.String) is not applicable 
    (actual argument RunnableThread cannot be converted to java.lang.ThreadGroup by method invocation conversion) 
constructor java.lang.Thread.Thread(java.lang.String) is not applicable 
    (actual and formal argument lists differ in length) 
constructor java.lang.Thread.Thread(java.lang.ThreadGroup,java.lang.Runnable) is not applicable 
    (actual argument RunnableThread cannot be converted to java.lang.ThreadGroup by method invocation conversion) 
constructor java.lang.Thread.Thread(java.lang.Runnable) is not applicable 
    (actual and formal argument lists differ in length) 
constructor java.lang.Thread.Thread() is not applicable 
    (actual and formal argument lists differ in length) 

我在这里使用相同的代码http://www.javabeginner.com/learn-java/java-threads-tutorial

我搜索了这个错误,但无法找到任何东西。

在此先感谢

回答

2

删除自己的Runnable接口的定义

7

您已经创建了自己的Runnable接口。我建议你删除它以避免混淆。

+0

非常感谢。它现在有效。 – Mariam 2011-12-14 13:22:05

-1
runner = new Thread(this, threadName); 

在这种情况下是RunnableThread。您需要通过螺纹延长RunnableThread或使用

Thread.currentThread() 

,而不是

+0

这是一个Runnable的实例,这很好。问题是他还创建了自己的Runnable接口,这显然是错误的。 – DaveJohnston 2011-12-14 13:48:41