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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2014-5-7 11:48  /  1914 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class ThreadDemo1//Java虚拟机加载类的字节码文件
{
        public static void main(String [] args)//找到主方法,开启主线程
        {
                new TestThread().start();//run();//创建线程并开启,这个线程为就绪状态,等待cpu分配执行权。cpu分配执行权是随机分配的,所以结果不确定。可以加入sleep();语句,让出执行权
                for (int x=0;x<5 ;x++ )
                {
                        System.out.println("main thread is running");
                }
                /*while (true)
                {
                        System.out.println("main thread is running");
                }
                */
        }
}
class TestThread extends Thread
{
        public void run()
        {
                for (int x=0;x<5;x++ )
                {
                        System.out.println(Thread.currentThread().getName()+" is running");
                }
                /*while (true)
                {
                        System.out.println(Thread.currentThread().getName()+" is running");
                }
                */
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马