2017-03-26 65 views
-1

我想從java.lang.Runnable實現Runnable的類的對象創建線程,但它不工作。使用Runnable對象創建線程

[email protected]:~/Desktop/java_prog$ javac Runnable.java 
Runnable.java:27: error: no suitable constructor found for Thread(counter,String) 
    Thread td=new Thread(obj,"bac"); 
      ^
constructor Thread.Thread(Runnable,AccessControlContext) is not applicable 
    (argument mismatch; counter cannot be converted to Runnable) 
constructor Thread.Thread(ThreadGroup,Runnable) is not applicable 
    (argument mismatch; counter cannot be converted to ThreadGroup) 
constructor Thread.Thread(ThreadGroup,String) is not applicable 
    (argument mismatch; counter cannot be converted to ThreadGroup) 
constructor Thread.Thread(Runnable,String) is not applicable 
    (argument mismatch; counter cannot be converted to Runnable) 
Note: Some messages have been simplified; recompile with - Xdiags:verbose to get full output 
1 error 

這是錯誤。 代碼如下.........

import java.io.*; 
interface Runnable{ 
public void run(); 
} 
class counter implements Runnable { 
Thread t=Thread.currentThread(); 
private int x; 
public counter(){x=0;} 
public int getval(){return x;} 
public void run() 
{ 
    try{ 
    while(x<5){ 
     System.out.println(t.getName()+ ":" + (x++)); 
     Thread.sleep(250); 
    } 
    System.out.println("Exit from thread:"+t);} 
    catch(InterruptedException e){ 
     System.out.println("InterruptedException"); 
    } 
} 
} 
class client { 
public static void main(String args[]) 
{ 
    counter obj=new counter(); 
    Thread td=new Thread(obj,"bac"); 
    td.start(); 
    int val; 
    try{ 
    do{ 
     val=obj.getval(); 
     System.out.println(td.getName()+ ":" + val); 
     val=6; 
     Thread.sleep(1000); 
    }while(val<5); 
    } 
    catch(InterruptedException e) 
    { 
     System.out.println("InterruptedException 2"); 
    } 
    System.out.println("Exit from thread:"+td.getName()); 
    } 
} 

我是不是要創建任何線程構造函數? 或我可以從java.lang庫訪問Runnable類? 這裏有什麼問題?

+0

第一種:已經存在預定義的接口Runnable。不要覆蓋預定義的名稱。然後。你在超級基本的東西上遇到了麻煩......你最好退後一步,在思考線程之前先處理更簡單的例子。對不起,但你的代碼是一大堆錯誤。 – GhostCat

+0

好的。我會改進它。 –

回答

0

我是否必須創建任何線程構造函數?或者我要從java.lang庫訪問 Runnable類?這裏有什麼問題?

你必須通過調用java.lang.Thread構造,並通過提供java.lang.Runnable類型的對象創建Thread對象。 我建議你看看Thread類構造器here,這樣你就可以發現你的代碼的問題。

有可用於創建Thread對象java.lang.Thread API構造函數的其他口味(超載),但與開始,你可以專注於Thread(Runnable target, String name)現在和理解(也就是你已經嘗試過使用什麼你的代碼)。

與您的代碼的問題是,您要創建自己的Runnable類型的對象,然後將它傳遞給java.lang.Thread的構造,因爲它的你所面臨的Thread.Thread(Runnable,AccessControlContext)不適用錯誤。

爲了解決這些問題,只需簡單地刪除Runnable接口定義並使用一個JDK即java.lang.Runnable。換句話說,java.lang.Thread類包含java.lang.Runnable接口的構造函數,並且Thread類不會接受您的Runnable對象。另外,請注意,您需要遵循java命名約定,即類名稱應該大寫(如class Counter而不是class counter)。

+0

thanx。 @javaguy。我使用java.lang.Runnable,它工作。 –

+0

我低調了我在做什麼錯誤......我感謝你的努力。謝謝你 。 –

0
import java.lang.Runnable; 
class counter implements Runnable 
{ 
    private int x; 
    public counter(){x=0;} 
    public int getval(){return x;} 
    public void run() 
    { 
     try{ 
     while(x<5){ 
      System.out.println(Thread.currentThread().getName()+ ":" + (x++)); 
      Thread.sleep(250); 
       } 
     System.out.println("Exit from thread:"+Thread.currentThread());} 
     catch(InterruptedException e){ 
     System.out.println("InterruptedException"); 
      } 
    } 
} 

public class client { 
    public static void main(String args[]) 
    { 
     counter obj=new counter(); 
     Thread td=new Thread(obj,"bac"); 
     td.start(); 
     int val; 
     try{ 
      do{ 
      val=obj.getval(); 
      System.out.println(td.getName()+ ":=" + val); 
      Thread.sleep(1000); 
      }while(val<5); 
      } 
     catch(InterruptedException e) 
     { 
       System.out.println("InterruptedException 2"); 
     }         
    System.out.println("Exit from thread:"+td.getName()); 
    } 
}