黑马程序员技术交流社区

标题: 大神们帮忙看看 [打印本页]

作者: NO?    时间: 2014-3-29 14:31
标题: 大神们帮忙看看

我理想的结果是test is operating!和main is operating!"交错,结果感觉还是单线程的运行结果,不过main is operating!"先运行了,想知道为什么不是交错运行?求各位大神解答,
  1. package threadtest;

  2. class TestThreads extends Thread{
  3.         public void run(){
  4.                 for(int i=0;i<=4;i++)
  5.                         System.out.println("test is operating!");
  6.         }
  7. }
  8. public class TestThread {
  9.         public static void main(String[] args) {
  10.                 new TestThreads().start();
  11.                 for(int i=0;i<=4;i++){
  12.                         System.out.println("main is operating!");
  13.                 }
  14.         }
  15. }
复制代码

作者: 黄晓鑫    时间: 2014-3-29 14:34
因为这是cpu自己选择执行的,你把run方法代码抽成一个函数,把main方法的代码抽成一个函数 然后在run方法写上一个死循环 调用这俩个方法 再那俩个方法加上互斥 通讯,这个就可以交错运行了
作者: 汗血黑马    时间: 2014-3-30 20:48
这应该和类加载器有关,main主函数是程序的入口,所以会先被加载进内存,执行里面的代码,执行start方法的时候才加载TestThreads类,运行线程。
作者: ╰つ    时间: 2014-3-30 22:55
你的电脑性能好了呗,cpu一刷就运行完了呗,你把循环次数搞高点,你就能看到效果了,或者在搞个sleep,让它睡一会
作者: 执笔梦    时间: 2014-3-31 11:07
循环次数有点少,搞多一点,因为类加载器会先加载main类,所以可能会先执行main中的循环.
作者: 霍振鹏    时间: 2014-3-31 12:31
这是我写的,真心觉得写的不好,不过功能实现了,,,,你可以参考下。。。
  1. package test;

  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;

  5. class TestThreads extends Thread{
  6.     public void run(){
  7.            
  8.             for(int i=0;i<=4;i++)
  9.             {
  10.                     TestThread.lock.lock();
  11.                     System.out.println("test is operating!");
  12.                     try {
  13.                             TestThread.condition1.signal();
  14.                                                 TestThread.condition2.await();
  15.                                                
  16.                                                 TestThread.lock.unlock();
  17.                                         } catch (InterruptedException e) {
  18.                                                 // TODO Auto-generated catch block
  19.                                                 e.printStackTrace();
  20.                                         }
  21.             }
  22.     }
  23. }
  24. public class TestThread {
  25.         static Lock lock=new ReentrantLock();
  26.         static Condition condition1=lock.newCondition();
  27.         static Condition condition2=lock.newCondition();
  28.         public static void main(String[] args) throws InterruptedException {
  29.                 new TestThreads().start();
  30.                 for(int i=0;i<=4;i++){
  31.                         lock.lock();
  32.                         condition1.await();
  33.                         System.out.println("main is operating!");
  34.                     condition2.signal();
  35.                     lock.unlock();
  36.                 }
  37.         }
  38.    
  39. }
复制代码

369.png (1.49 KB, 下载次数: 23)

369.png





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2