通过扩展Thread类来创建线程 [英] Thread creation by extending Thread class

查看:154
本文介绍了通过扩展Thread类来创建线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有关通过扩展Thread类创建线程的简单示例.

This is a simple example about creating a thread by extending the Thread class.

class Count extends Thread {

    Count() {
        super("my extending thread");
        System.out.println("my new thread is started " + this);
        start();
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("count " + i);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("my thread run is over");
        }
    }

}

public class Multi2 {

    public static void main(String[] args) {
        Count c = new Count();
        try {
            while (c.isAlive()) {
                System.out.println("main thread is alive untill child thread is alive");
                Thread.sleep(1500);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("main thread is over");
        }
    }
}

我的输出是这个.

my new thread is started Thread[my extending thread,5,main]
main thread is alive untill child thread is alive
count 0
count 1
main thread is alive untill child thread is alive
count 2
main thread is alive untill child thread is alive
count 3
count 4
main thread is alive untill child thread is alive
count 5
main thread is alive untill child thread is alive
count 6
count 7
main thread is alive untill child thread is alive
count 8
main thread is alive untill child thread is alive
count 9
my thread run is over
main thread is over

我的问题是


01. main thread is alive untill child thread is alive输出如何在
count 0 count 1之前打印


01. How come main thread is alive untill child thread is aliveoutput printed before
count 0 count 1

02.为什么main thread is alive untill child thread is alive输出与run()方法的输出一起继续打印?

02.How come main thread is alive untill child thread is aliveoutput keep printing withing the output of run() method?

请帮助我解决这个问题.
谢谢.

Please help me to figure this out.
Thank you.

推荐答案

计数有这一行:

Thread.sleep(1000);

您的主程序有这一行:

Thread.sleep(1500);

很显然,您将获得每3计数打印2张主打印.这就是0和1彼此相邻的原因.

Clearly, you're going to get 2 main prints for every 3 count prints. That's why 0 and 1 are next to each other.

关于为什么要在计数打印之前看到主打印的原因,您可以看到以下内容:

As for why you see the main print before the count prints, you can see this:

Count c = new Count();
try {
while (c.isAlive()) {
    System.out.println("main thread is alive untill child thread is alive");
    Thread.sleep(1500);

您已经关闭了c,但是在JVM执行上下文切换以实际运行该线程之前,您可能看不到结果.事实是,在某些系统上,您可能会在此之前查看计数器.通常,因为它离开始时还很近并且还没有屈服,所以您会在柜台前看到主要印刷品.

You've fired off your c but until your JVM performs a context switch to actually run that thread, you might not see results. The truth is you may, on some systems, see your counter before that. Often, because it's so close to when it kicks off and hasn't yielded yet, you'll see the main print before the counter.

对于第二部分,main thread is...继续打印,因为它有一个循环,告诉您可以打印直到计数器线程不再存在为止.它们都使用System.out,因此当您查看控制台时,它们都在那看到.

For your second part, your main thread is... keeps printing because it has a loop that tells it to print until your counter thread is no longer alive. They both use System.out so when you look at your console you see them both there.

这篇关于通过扩展Thread类来创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆