2017-03-26 3 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(Runnable target, String name)에 집중하고 그것이 당신이 사용하려고 한 것입니다 (그것을 이해할 수있다 Thread 개체를 만드는 데 사용할 수 있습니다 java.lang.Thread API의 생성자 (과부하) 다른 맛이지만, 시작하기 귀하의 코드).

코드와 문제는 당신이 적용 오류를 자신의 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()); 
    } 
}