java吧 关注:1,291,872贴子:12,821,353
  • 3回复贴,共1

自学者请一个关于多线程yield();方法的问题

只看楼主收藏回复

class MultiThread
{
public static void main(String[] args) 
{
MyThread mt=new MyThread();
//使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
mt.start();

for(int i=0;i<20;i++)
//返回该线程的名称。
System.out.println("main:"+Thread.currentThread().getName());
}
}

class MyThread extends Thread
{
public void run()
{
yield();
for(int i=0;i<20;i++)
System.out.println("Thread Print");
}
}

请教下我在Point类中加入yield();,为什么还会打印"Thread Print"?

刚自学学到多线程这里,请大家多多指教


IP属地:北京1楼2008-05-23 20:03回复
    • 219.245.174.*
    yield只是让当前运行的线程“休息”一下,让其他正在等待的同优先级的线程先运行,然后再运行这个线程。所以这个线程还是要运行的。并且如果等待的线程优先级都要低于当前线程,那么下一个运行可能仍是这个线程


    2楼2008-05-24 08:29
    回复
      2025-12-20 02:02:59
      广告
      不感兴趣
      开通SVIP免广告
      迟到的谢谢,请你不要介意

      我后来自己看书也看懂


      IP属地:北京3楼2008-06-01 08:48
      回复
        • 116.116.32.*
        for the specified number of milliseconds. The thread does not lose ownership of any monitors.
        源带代码中说它使线程暂停几毫秒.


        4楼2008-06-01 16:45
        回复