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 클래스에 액세스하려면 ?? 여기에 무슨 문제가 있습니까 ??
첫 번째 : 미리 정의 된 기존 인터페이스 인 Runnable이 이미 있습니다. 미리 정의 된 이름을 덮어 쓰지 마십시오. 그때. 당신은 슈퍼 기본 물건에 문제가 있습니다 ... 당신은 더 나은 단계를 뒤로하고 더 간단한 예제에서 작업하기 전에 스레드에 대해 생각합니다. 죄송하지만 코드가 실수로 엉망입니다. – GhostCat
좋습니다. 나는 그것을 향상시킬 것이다. –