A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 武腊福 中级黑马   /  2015-5-14 20:43  /  563 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Demo extends Thread
{
        public void run()
        {
                for(int x=0;x<20;x++)
                {
                        System.out.println(Thread.currentThread().getName()+"......"+x);
                }
        }
}
class ThreadDemo7
{
        public static void main(String[] args)
        {
                Demo d1=new Demo();
                Demo d2=new Demo();
                d1.start();
                d2.run();
        }
}
如果改成d1.run();d2.start();
结果为什么会不一样?

2 个回复

倒序浏览
知识点介绍:main()方法里跑的是主线程。
问题说明:
d1.start();是开启一个新线程去运行run()方法内的内容。Thread.currentThread()获得的是子线程
d2.run();是主线程中d2对像调用run()方法运行内部内容。Thread.currentThread() 获得的是主线程
结论:
线程不是一个所以线程名字不一样
回复 使用道具 举报
学习一下,,,,,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马