java中多线程编程错误

时间:2015-08-26 10:11:29

标签: java multithreading

当我运行给定的多线程代码时,我遇到了这个问题,无法理解为什么.. 我提供代码以及输出截图。

var classesArray = classes.split(' ');

1 个答案:

答案 0 :(得分:4)

问题是您使用的是Thread类,并且没有名为start()的方法。 将类名从Thread更改为MyThread,它应该可以工作。

以这种方式改变它:

class A extends Thread {
    public void run() {
        for (int i = 1; i < 6; i++) {
            if (i == 1) {
                yield();
            }
            System.out.println("\tFrom Thread A i: " + i);
        }
        System.out.println("Exit from A");
    }
}

class B extends Thread {
    public void run() {
        for (int j = 1; j < 6; j++) {
            System.out.println("\tFrom Thread B j: " + j);
            if (j == 3)
                stop();
        }
        System.out.println("Exit from B");
    }
}

class C extends Thread {
    public void run() {
        for (int k = 1; k < 6; k++) {
            System.out.println("\tFrom Thread C k: " + k);
            if (k == 1)
                try {
                    sleep(1000);
                } catch (Exception e) {
                }
        }
        System.out.println("Exit from C");
    }
}

public class MyThread {

    public static void main(String args[]) {
        A threadA = new A();
        B threadB = new B();
        C threadC = new C();
        System.out.println("Start Thread A");
        threadA.start();
        System.out.println("Start Thread B");
        threadB.start();
        System.out.println("Start Thread C");
        threadC.start();
        System.out.println("End of main thread");
    }

}