Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java runnable中需要类、接口或枚举_Java_Multithreading - Fatal编程技术网

Java runnable中需要类、接口或枚举

Java runnable中需要类、接口或枚举,java,multithreading,Java,Multithreading,这是一个实现runnable的简单程序。我得到一份工作 import java.util.*; class NewThread implements Runnable{ Thread t; NewThread(){ t = new Thread(this, "Demo Thread"); System.out.println("Child Thread "+t); t.start(); } } public void

这是一个实现runnable的简单程序。我得到一份工作

import java.util.*;


class NewThread implements Runnable{
    Thread t;

    NewThread(){
        t = new Thread(this, "Demo Thread");
        System.out.println("Child Thread "+t);
        t.start();
    }
}

public void run(){
    try{
        for(int i=5;i>0;i--){
            System.out.println("Child Thread:"+i);
            Thread.sleep(1000);
        }
    } catch(InterruptedException e){
        System.out.println("Child Interrupted");
    }

    System.out.println("Child Thread Exiting\n");
}


public class ThreadDemo{
    public static void main(String[] args) {
        thread curr = thread.currentThread();
        System.out.println("Current Thread"+curr);  
        new NewThread();

        try{
            for(int i=0;i>5;i--){
                System.out.println("Parent Thread"+i);
                Thread.sleep(1000);
            }
        } catch(InterruptedException e){
            System.out.println("Main thread interrupted");
        }
        System.out.println("Main Thread Exiting");      
    }
}
我在编译时遇到这些错误

java:14:需要类、接口或枚举

  public void run(){
   ^
  for(int i=5;i>0;i--){
              ^
      Thread.sleep(1000);             ^
java:16:需要类、接口或枚举

  public void run(){
   ^
  for(int i=5;i>0;i--){
              ^
      Thread.sleep(1000);             ^
java:16:需要(int)的类、接口或枚举 i=5;i>0;i--){ ^

java:18:需要类、接口或枚举

  public void run(){
   ^
  for(int i=5;i>0;i--){
              ^
      Thread.sleep(1000);             ^
java:19:应为类、接口或枚举}^

java:22:应为类、接口或枚举}^

java:25:需要类、接口或枚举

  public void run(){
   ^
  for(int i=5;i>0;i--){
              ^
      Thread.sleep(1000);             ^
}


在包含
run
方法之前,您已经关闭了类的定义。请点击此处:

class NewThread implements Runnable{
    Thread t;

    NewThread(){
        t = new Thread(this, "Demo Thread");
        System.out.println("Child Thread "+t);
        t.start();
    }
} // <-- remove this
public void run() {
    //implementation...
}
//<-- add the } here
//rest of your code...
您的代码应该是这样的:

import java.util.*;

class NewThread implements Runnable{
    //a Runnable shouldn't have another thread
    NewThread() {
    }
}

public void run(){
    for (int i=5;i>0;i--) {
        System.out.println("Child Thread:"+i);
        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) {
            //interrupted exception will somewhat wake the thread up
            //so it must handle the Thread#sleep method only.
            System.out.println("Child Interrupted");
        }
    }
    System.out.println("Child Thread Exiting");
}

public class ThreadDemo{
    public static void main(String[] args) {
        thread curr = thread.currentThread();
        System.out.println("Current Thread"+curr);
        //pass the runnable to a thread and start the thread
        new Thread(new NewThread()).start();
        for (int i=0;i>5;i--) {
            System.out.println("Parent Thread"+i);
            try {
                Thread.sleep(1000);
            } catch(InterruptedException e){
                System.out.println("Main thread interrupted");
            }
        }
        System.out.println("Main Thread Exiting");      
    }
}

您的
run
方法在类外部

您需要将
run()
方法放在
NewThread

类内部,方法应包含在类中。您已将结束大括号“}”放在run方法之前

run()
方法应该位于
NewThread
类的内部。您在构造函数后面添加了一个额外的
}
来关闭这个类。另一个很好的例子是为什么您应该使用功能齐全的IDE来开发Java…@rajsherror make it work并不意味着您正在学习。请注意我的答案中的更新,以了解如何使用
线程
可运行
@LuiggiMendoza。我将添加一个指向Oracle Concurence教程的链接。很好的编辑,但是你没有解释为什么会这样,或者你的编辑对操作码做了什么改变。这在我制作的图片中有解释。顺便说一下,因为我的答案是CW,任何人都可以自由编辑它。
class NewThread implements Runnable{
    Thread t;

    NewThread(){
        t = new Thread(this, "Demo Thread");
        System.out.println("Child Thread "+t);
        t.start();
    }
// modify remove this }     
//}

    public void run(){
        try{
            for(int i=5;i>0;i--){
                System.out.println("Child Thread:"+i);
                Thread.sleep(1000);
            }
        } catch(InterruptedException e){
            System.out.println("Child Interrupted");
        }

        System.out.println("Child Thread Exiting\n");
    }
// modify add this }    
}

public class ThreadDemo{
    public static void main(String[] args) {
        //modify thread curr = thread.currentThread();
        Thread curr = Thread.currentThread();
        System.out.println("Current Thread"+curr);  
        new NewThread();

        try{
            for(int i=0;i>5;i--){
                System.out.println("Parent Thread"+i);
                Thread.sleep(1000);
            }
        } catch(InterruptedException e){
            System.out.println("Main thread interrupted");
        }
        System.out.println("Main Thread Exiting");      
    }
}