11
public class CreateThreadRunnableExample implements Runnable {

    public void run() {

        for (int i = 0; i < 5; i++) {
            System.out.println("Child Thread : " + i);

            try {
                Thread.sleep(50);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }

        System.out.println("Child thread finished!");
    }

    public static void main(String[] args) {

        Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");

        t.start();

        for (int i = 0; i < 5; i++) {

            System.out.println("Main thread : " + i);

            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }
        System.out.println("Main thread finished!");
    }
}

在这个程序中,使用了两种不同时间的睡眠方法..,,,所以如果主线程运行时间那么子线程必须运行2次。但它只运行一次......我们采用可运行或运行的概念状态......然后当主线程结束时,2个子线程将处于就绪状态,那么为什么只有一个子线程运行。

4

4 回答 4

3

首先你添加了 System.out.println("子线程中断!" + ie); 对于主线程和子线程,这是一个错字...

试试这个......两个线程都在运行(主线程和子线程)

该程序的main方法放在JVM创建的Main线程的最底层,Main方法创建另一个Runtime Stack并将子线程放入其中。

public class DemoThread implements Runnable {

    public void run() {

        for (int i = 0; i < 3; i++) {
            System.out.println("Child Thread ");

            try {
                Thread.sleep(200);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }

        System.out.println("Child thread finished!");
    }

    public static void main(String[] args) {

        Thread t = new Thread(new DemoThread ());

        t.start();

        for (int i = 0; i < 3; i++) {

            System.out.println("Main thread);

            try {
                Thread.sleep(200);
            } catch (InterruptedException ie) {
                System.out.println("Child thread interrupted! " + ie);
            }
        }
        System.out.println("Main thread finished!");
    }
}
于 2012-05-22T07:27:31.347 回答
2

如JavaDoc中所述,该sleep方法并非 100% 时间准确:

公共静态无效睡眠(长毫秒)抛出 InterruptedException

使当前执行的线程休眠(暂时停止执行)指定的毫秒数,取决于系统计时器和调度程序的精度和准确性。该线程不会失去任何监视器的所有权。

所以基本上代码有时可能会按预期工作,而其他时候则不会。

此外,您似乎只开始了 1 个孩子,那么您为什么要 2 个孩子呢?

main编辑:理论上是的,子线程应该在线程睡着时执行两次。话虽如此,正如 JavaDoc 中所解释的,该sleep方法执行的准确性取决于它所运行的系统,因此有时您可能会使其按预期工作,而有时则不会。因此,例如,您可能会遇到main线程休眠97 毫秒而不是 100 毫秒的情况,而子线程休眠 53 毫秒。这将导致孩子只执行一次。

另一种选择是做这样的事情: while (currentTime <= timeNeededToElapse) { currentTime=... }. 这会导致一个紧密的 while 循环,它可以提供更好的控制,但据我所知,它仍然不是 100% 准确的,更不用说你将消耗 CPU 周期来有效地什么都不做,所以要谨慎使用它。

于 2012-05-22T07:12:04.197 回答
1

您只启动一个子线程,那么为什么要期待两个?

关于超时,睡眠时间并不准确,因此当一个线程睡眠 100 毫秒时,另一个线程可能会或可能不会睡眠两次 50 毫秒。这是竞态条件的经典示例。如果您多次运行示例代码,您可能会在某些情况下看到两条子线程消息,而在其他情况下会看到一条。

于 2012-05-22T07:08:32.497 回答
0

试试这个

  public class Test implements Runnable{

        public void run() {

            for (int i = 0; i < 5; i++) {
                System.out.println("CHILD THREAD : " + i);

                try {
                    Thread.sleep(50);
                } catch (InterruptedException ie) {
                    System.out.println("CHILD THREAD INTERRUPTED! " + ie);
                }
            }

            System.out.println("CHILD THREAD FINISHED!");
        }

        public static void main(String[] args) {

            Thread t = new Thread(new Test());

            t.start();

            for (int i = 0; i < 5; i++) {
                System.out.println("MAIN THREAD : " + i);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ie) {
                    System.out.println("CHILD THREAD INTERRUPTED! " + ie);
                }
            }
            System.out.println("MAIN THREAD FINISHED!");
        }
    }
于 2017-05-24T13:01:53.783 回答