2011-12-14 35 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