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

を使用していますが、何かを見つけることができませんでした。

ありがとうございます。

答えて

7

独自のRunnableインターフェイスを作成しました。私はあなたが混乱を避けるためにそれを削除することをお勧めします。この場合

+0

を見ます本当にありがとう。それは今働く。 – Mariam

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

このはRunnableThreadです。あなたは、Runnableインタフェースの独自の定義を削除スレッドによってRunnableThreadを拡張したり

Thread.currentThread() 

の代わりに、この

+0

これはRunnableのインスタンスですが、これは問題ありません。問題は明らかに間違った独自のRunnableインターフェースも作成していることです。 – DaveJohnston

3

.. Javaでマルチスレッドを実装するには2つの方法があります

  1. Runnableインターフェイスを実装する
  2. Threを拡張する広告クラス

アプローチは彼らの長所と短所を持っている...あなたはあなた自身、Runnableインタフェースを必要といけない両...それがすでに提供されて...次のリンク

Multithreading tutorial

Runnable vs Thread