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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄玉昆 黑马帝   /  2013-2-17 18:29  /  1562 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黄玉昆 于 2013-2-18 14:51 编辑

我看到毕老师将多线程优先级的视频(第十二天最后一个视频)后,试了试毕老师给的程序,就是匿名内部类的那个。
这几个线程位置不同,结果也不同,尤其是第二种写法,一直是线程Thread-1最后再执行,屡试不爽啊,让我有些不爽了,不知道原因何在?
第一种:
  1. <font size="1" face="Arial">class ThreadTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 new Thread()
  6.                 {
  7.                         public void run()
  8.                         {
  9.                                 for (int x=0;x<100;x++)
  10.                                 {
  11.                                         System.out.println(Thread.currentThread().getName() + "----" + x);
  12.                                 }
  13.                         }
  14.                 }.start();

  15.                 Runnable r = new Runnable()
  16.                 {
  17.                         public void run()
  18.                         {
  19.                                 for (int x=0;x<100;x++)
  20.                                 {
  21.                                         System.out.println(Thread.currentThread().getName() + "----" + x);
  22.                                 }
  23.                         }
  24.                 };
  25.                 new Thread(r).start();
  26.                
  27.                 for (int x=0;x<100;x++)
  28.                 {
  29.                         System.out.println(Thread.currentThread().getName() + "----" + x);
  30.                 }
  31.         }
  32. }
  33. </font>
复制代码
第二种:
  1. <font size="1" face="Arial">class ThreadTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 new Thread()
  6.                 {
  7.                         public void run()
  8.                         {
  9.                                 for (int x=0;x<100;x++)
  10.                                 {
  11.                                         System.out.println(Thread.currentThread().getName() + "----" + x);
  12.                                 }
  13.                         }
  14.                 }.start();

  15.                 for (int x=0;x<100;x++)
  16.                 {
  17.                         System.out.println(Thread.currentThread().getName() + "----" + x);
  18.                 }

  19.                 Runnable r = new Runnable()
  20.                 {
  21.                         public void run()
  22.                         {
  23.                                 for (int x=0;x<100;x++)
  24.                                 {
  25.                                         System.out.println(Thread.currentThread().getName() + "----" + x);
  26.                                 }
  27.                         }
  28.                 };
  29.                 new Thread(r).start();
  30.         }
  31. }</font>
复制代码
是不是大家运行的结果都是如此呢?有不同的结果的附上图片啊,谢谢


8 个回复

正序浏览
黄玉昆 发表于 2013-2-18 14:52
嗯啊,理解多线程就行了。不纠结了。嘿嘿

好的,你很积极的,表示佩服,一起加油。
回复 使用道具 举报
黄玉昆 黑马帝 2013-2-18 14:52:15
8#
陈科宇 发表于 2013-2-18 14:32
朋友,为这个问题纠结太不值得。

嗯啊,理解多线程就行了。不纠结了。嘿嘿
回复 使用道具 举报
朋友,为这个问题纠结太不值得。
回复 使用道具 举报
范天成 发表于 2013-2-17 21:39
表示你那个纯属太“和谐”了……
楼主电脑几核的?

我的是三核的啊,看来我还得再试试
回复 使用道具 举报
本帖最后由 范天成 于 2013-2-17 21:43 编辑

E:\code\QQ截图20130217213559.jpg
表示你那个纯属太“和谐”了……
楼主电脑几核的?

QQ截图20130217213559.jpg (27.07 KB, 下载次数: 53)

QQ截图20130217213559.jpg
回复 使用道具 举报
其实多线程是系统自动调用的,肯定会有时间上的不同,程序员只能手动控制谁先谁后,可以用Thread。sleep(2000);让他睡会,数值是毫秒值,

第二个里你的程序总是Thread-1最后运行,因为,主线程执行到第一个new Thread().start();时候先开辟了一个空间运行的就是Thread-0;然后主线程紧接着就开始忙乎for循环了,等循环完了,才开始了执行创建Thread-1,所以总是Thread-1最后运行。
回复 使用道具 举报
刘军亭 发表于 2013-2-17 20:16
你可以设置一下线程的优先级试试,还可以让某个线程睡会在运行。

这样不太好吧,让线程睡多久呢?那就得用等待唤醒机制了。就这个程序而言,为什么会出现这种情况。很伤脑筋啊
回复 使用道具 举报
你可以设置一下线程的优先级试试,还可以让某个线程睡会在运行。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马