简体   繁体   中英

Java Thread program not working

I coded a sample java thread programm, But it shows Error as follows

java.lang.Error: Unresolved compilation problem: The constructor Thread(Test) is undefined

Showing Test class is not a runnable class. I refered many sites but didn't get the solution and its showing what i had done is correct.
my java code:

public class ThreadDemo {  
       public static void main(String args[]) {  
        new Thread(new Test()).start();
    }
}

Test.java class file implements Runnable , is in a jar file and I included as a library in my project in eclipse . Is There Any issue with that ?
Test.java

public class Test implements Runnable {  
       @Override public void run() {  
        Sysout("Test");
    }
}

Test class contains many other methods those are using many other classes from other jars in project but run method is not calling any of these method in it. Any problem with these factors ?

This thread demo is working well with another class implements Runnable containing run() with sysout() only. But as i said my Test class containing many methods which further refers other jars and but its not called in run() method of Test class till now. when i used new Thread(new Test()).start(); Eclipse showing illegal argument or Cast to Runnable argument suggestion with error. Then I put that casting and got above error. Is there any Issue related with running threads of a class which using any types of jars and variables?

Thanks in advance.

In my estimate, the files listed above are correctly formed. The problem could perhaps be caused by having another file called "Test.java" in your class path in a jar file. It seems unlikely, but I put the listed classes into my compiler and they work, so the problem seems like it must be elsewhere.

Rename your Test.java and see if you still have the problem.

The Thread class constructor requires Runnable object (any class implementing Runnable interface) as a parameter. This compilation error suggests that your parameter class does not implement Runnable interface.

If you do the needful, the compiler wont complain.

Example:

public class Test implements Runnable { // Will make this class Runnable
@override
public void run() { // Body of the thread
// work the thread needs to do
}
}

Your class should be declared to implement Runnable , like this:

class Test implements Runnable { 
  @Override
  public void run() {
    ...
  }
}

查找其中存在Test类的第三方jar的编译级别,以及用于程序的编译目标。

So your test class should look like

public Class Test implements Runnable{
@Override
public void run(){
     // TODO this auto generated code 
    }
}

So you when you call start() method, new thread will start executing from the run() method

Another Approach:

You can extend the Thread class as

 public Class Test extends Thread{
    @Override
    public void run(){
         // TODO this auto generated code 
        }
    }

But it not used usually as only one class can be extended in java. So if you extends the Thread class then you can not extends any other class in your application.

Yet Another Approach:

You can also implements the callable interface same as in the first case with the Runnable interface as

public Class Test implements Callable{
@Override
public long call(){
     // TODO this auto generated code 
     long a= 3.14xxxxxxxx;
     return a;  
    }
}

The main difference is that, In Runnable interface, you can not return any thing from run method ie return type is void

while in the Callble interface, call method can return values as in the above example(long a)`

To create a thread you need to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.

here in your code you need a class like this.

class Test implements Runnable {
..
}

then you need to allocate a new thread object and start it, you can do this.

Thread(new Test()).start();

Look here for more details

Instead of instantiating a new Thread object, you can do new Test().schedule(); It will launch as a thread by itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM