这段代码中有什么错误

时间:2015-09-04 03:22:45

标签: java multithreading extends

class thread123 extends Thread {

    public thread123(int ind, int interval) {
        i0 = ind;
        delay = interval;
    }

    public void run() {

        try {
            for (int i = 1; i <= 10; i++) {
                if (i0 == 1) {
                    System.out.println("n = " + i + ", Time = " + (delay * i - delay) + ", Thread one: " + (2 * i + 5));
                } else if (i0 = 2) {
                    System.out.println("n = " + i + ", Time = " + (delay * i - delay) + ", Thread Two: " + (i + 10) * 2);
                } else {
                    System.out.println("n = " + i + ", Time = " + (delay * i - delay) + ", Thread Three: " + (18 * (i * i) - 12) / (i - 2);
                }
                sleep(delay);
            }

        } catch (InterruptedException e) {
            return;
        }

    }

    private int i0;
}

class Threads {

    public static void main(String[] args) throws InterruptedException {
        thread123 t1 = new thread123(1, 20);
        thread123 t2 = new thread123(2, 3.0);
        thread123 t3 = new thread123(3, 40);
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println("Main thread done");
    }

}

1 个答案:

答案 0 :(得分:1)

  • thread123 t2 = new thread123(2, 3.0);无效,因为thread123只需int s
  • delay = interval;无效,因为delay未定义,因此与delay有关的任何内容都与此相关
  • } else if (i0 = 2) {是和作业,而不是评估,它不等同于boolean,因此无效
  • System.out.println("n = " + i + ", Time = " + (delay * i - delay) + ", Thread Three: " + (18 * (i * i) - 12) / (i - 2);错过了结束)

您可能还希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码