-1

This is a simple program for implementing runnable. i'm getting an

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");      
    }
}

i get these errors while compiling

ThreadDemo.java:14: class, interface, or enum expected

  public void run(){
   ^

ThreadDemo.java:16: class, interface, or enum expected

  for(int i=5;i>0;i--){
              ^

ThreadDemo.java:16: class, interface, or enum expected for(int i=5;i>0;i--){ ^

ThreadDemo.java:18: class, interface, or enum expected

      Thread.sleep(1000);             ^

ThreadDemo.java:19: class, interface, or enum expected } ^

ThreadDemo.java:22: class, interface, or enum expected } ^

ThreadDemo.java:25: class, interface, or enum expected

}

4

5 回答 5

1

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...

除了其他人指出的臭名昭著的错误之外,您的设计很奇怪。ARunnable不应该包含一个Thread来执行其作业,因为Runnable它将由另一个执行Thread。这是您的线程当前设计(或其行为方式)的概述:

Main thread
    |
    | ---->   new NewThread(pass the Runnable)
    |              |
    |              |
    |              |
    |              |  ----------------------->  new Thread().start()
    |              |                                     |
    |              |                                     |
    .              .                                     .
    .              .                                     .
    .              .                                     .

这就是您的代码的样子:

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");      
    }
}
于 2013-09-23T14:13:09.757 回答
1

你需要把你的run()方法放在你的NewThread班级里

于 2013-09-23T14:13:34.383 回答
0
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");      
    }
}
于 2013-09-23T14:22:36.997 回答
0

你的run方法在课堂之外

于 2013-09-23T14:13:33.680 回答
0

方法应该包含在类中。您已将结束大括号 '}' 放在 run 方法之前。

于 2013-09-23T14:14:13.997 回答