简体   繁体   English

Java线程程序不起作用

[英]Java Thread program not working

I coded a sample java thread programm, But it shows Error as follows 我编写了一个示例Java线程程序,但是它显示Error如下

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

Showing Test class is not a runnable class. 显示Test类不是可运行的类。 I refered many sites but didn't get the solution and its showing what i had done is correct. 我推荐了许多网站,但没有得到解决方案,它显示出我所做的工作是正确的。
my java code: 我的Java代码:

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 . Test.java类文件实现了Runnable ,位于jar文件中,我作为库包含在我的eclipse项目中。 Is There Any issue with that ? 有什么问题吗?
Test.java 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. 测试类包含许多其他方法,这些方法正在使用项目中其他jar中的许多其他类,但run方法未在其中调用任何这些方法。 Any problem with these factors ? 这些因素有什么问题吗?

This thread demo is working well with another class implements Runnable containing run() with sysout() only. 该线程演示与另一类仅包含sysout()的包含run()的Runnable实现良好配合。 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. 但是正如我所说的,我的Test类包含许多进一步引用其他jar的方法,但是到目前为止,在Test类的run()方法中尚未调用它。 when i used new Thread(new Test()).start(); 当我使用new Thread(new Test())。start(); Eclipse showing illegal argument or Cast to Runnable argument suggestion with error. Eclipse显示非法参数或带有错误的Cast to Runnable参数建议。 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? 是否存在与使用任何类型的jar和变量的类的运行线程相关的问题?

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. 该问题可能是由于jar文件中的类路径中包含另一个名为“ Test.java”的文件引起的。 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. 重命名您的Test.java,看看是否仍然有问题。

The Thread class constructor requires Runnable object (any class implementing Runnable interface) as a parameter. Thread类的 构造函数需要Runnable对象 (任何实现Runnable接口的类)作为参数。 This compilation error suggests that your parameter class does not implement Runnable interface. 此编译错误表明您的参数类未实现Runnable接口。

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: 应该声明您的类以实现Runnable ,如下所示:

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 因此,当您调用start()方法时,新线程将从run()方法开始执行

Another Approach:

You can extend the Thread class as 您可以extend the Thread class

 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. 但是它通常不使用,因为在Java中只能扩展一个类。 So if you extends the Thread class then you can not extends any other class in your application. 因此,如果扩展Thread类,则无法扩展应用程序中的任何其他类。

Yet Another Approach:

You can also implements the callable interface same as in the first case with the Runnable interface as 您还可以使用Runnable接口来实现与第一种情况相同的可调用接口

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 主要区别在于,在Runnable接口中,您无法从run方法返回任何东西,即return type is void

while in the Callble interface, call method can return values as in the above example(long a)` 而在Callble接口中, call method can return values如上例中的call method can return values (long a)

To create a thread you need to declare a class that implements the Runnable interface. 要创建线程,您需要声明一个实现Runnable接口的类。 That class then implements the run method. 然后,该类实现run方法。 An instance of the class can then be allocated, passed as an argument when creating Thread, and started. 然后可以分配该类的实例,在创建Thread时将其作为参数传递并启动。

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(); 无需实例化新的Thread对象,您可以执行new Test()。schedule();。 It will launch as a thread by itself. 它会自己作为线程启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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