类型不匹配:无法从void转换为Thread

时间:2023-01-12 13:20:38

I am trying to make a program in java the accepts any connection but I getting this error:

我试图在java中创建一个程序接受任何连接,但我收到此错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Type mismatch: cannot convert from void to Thread

at ojas.gome.Server.<init>(Server.java:37)
at ojas.gome.Server.main(Server.java:12)

This piece of code gives me this error:

这段代码给了我这个错误:

clientaccepter = new Thread(new Runnable() {
            public void run() {
                while(true) {
                    try {
                        server.accept();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "clientaccepter").run();

Please tell me if I left out anything needed.

请告诉我是否遗漏了所需的任何东西。

2 个解决方案

#1


1  

You're setting a variable equal to the result of the run method which does not return a value - it's void.

您设置的变量等于run方法的结果,该方法不返回值 - 它是无效的。

clientaccepter = new Thread....run();

clientaccepter = new Thread .... run();

You should declare the thread and then start it on a separate line if you need to retain a reference to it. Also, you should use start to begin a new Thread not run. Please see Defining and Starting a Thread. Lastly, variables should be camel case starting with lowercase - please see Java Naming Conventions.

如果需要保留对它的引用,则应声明该线程,然后在单独的行上启动它。此外,您应该使用start来开始新的Thread不运行。请参阅定义和启动线程。最后,变量应该是以小写字母开头的驼峰式案例 - 请参阅Java命名约定。

clientAccepter = new Thread(...);
clientAccepter.start();

#2


0  

  1. either go for complete statement Thread t = new Thread( new Runnable () ) and call t.run() or
  2. 要么去完整语句线程t =新线程(新的Runnable())并调用t.run()或

  3. create directly new Thread() (new Runnalbe()) on that call start() as follows :

    在该调用start()上直接创建新的Thread()(new Runnalbe()),如下所示:

    public class InterfaceDemo {
    
        public static void main(String[] args) {
    
         new Thread(new Runnable() 
        {
            @Override
            public void run() 
            {
                for(int i=0;i<=10;i++) {
                    System.out.println("run() : "+i);
                }
            }
        }).start();
    
            for(int i = 1; i<=10; i++) {
                System.out.println("main() : "+i);
            }
        }// main
    } // class
    

#1


1  

You're setting a variable equal to the result of the run method which does not return a value - it's void.

您设置的变量等于run方法的结果,该方法不返回值 - 它是无效的。

clientaccepter = new Thread....run();

clientaccepter = new Thread .... run();

You should declare the thread and then start it on a separate line if you need to retain a reference to it. Also, you should use start to begin a new Thread not run. Please see Defining and Starting a Thread. Lastly, variables should be camel case starting with lowercase - please see Java Naming Conventions.

如果需要保留对它的引用,则应声明该线程,然后在单独的行上启动它。此外,您应该使用start来开始新的Thread不运行。请参阅定义和启动线程。最后,变量应该是以小写字母开头的驼峰式案例 - 请参阅Java命名约定。

clientAccepter = new Thread(...);
clientAccepter.start();

#2


0  

  1. either go for complete statement Thread t = new Thread( new Runnable () ) and call t.run() or
  2. 要么去完整语句线程t =新线程(新的Runnable())并调用t.run()或

  3. create directly new Thread() (new Runnalbe()) on that call start() as follows :

    在该调用start()上直接创建新的Thread()(new Runnalbe()),如下所示:

    public class InterfaceDemo {
    
        public static void main(String[] args) {
    
         new Thread(new Runnable() 
        {
            @Override
            public void run() 
            {
                for(int i=0;i<=10;i++) {
                    System.out.println("run() : "+i);
                }
            }
        }).start();
    
            for(int i = 1; i<=10; i++) {
                System.out.println("main() : "+i);
            }
        }// main
    } // class