java的join和yield方法处理线程顺序执行

http://www.importnew.com/14958.html

http://blog.sina.com.cn/s/blog_5c5bc9070100ytye.html

Thread的非静态方法join()让一个线程B“加入”到另外一个线程A的尾部。在A执行完毕之前,B不能工作。例如:
        Thread t = new MyThread();
        t.start();
        t.join();
另外,join()方法还有带超时限制的重载版本。 例如t.join(5000);则让线程等待5000毫秒,如果超过这个时间,则停止等待,变为可运行状态。
 
线程的加入join()对线程栈导致的结果是线程栈发生了变化,当然这些变化都是瞬时的。
public class TestJoin {
 
  public static void main(String[] args) {
   
    MyThread2 t1 = new MyThread2("TestJoin");
    t1.start();
    try {
      t1.join();  //join()合并线程,子线程运行完之后,主线程才开始执行
     }catch (InterruptedException e) {  }
      
     for(int i=0 ; i <10; i++)
              System.out.println("I am Main Thread");
   }
 }
 
 class MyThread2 extends Thread {
  
    MyThread2(String s) {
     super(s);
     }
     
  public void run() {
    for(int i = 1; i <= 10; i++) {
     System.out.println("I am "+getName());
     try {
      sleep(1000); //暂停,每一秒输出一次
      }catch (InterruptedException e) {
      return;
     }
     }
   }
  }
程序运行结果:
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am TestJoin
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread
I am Main Thread


yield()方法

理论上,yield意味着放手,放弃,投降。一个调用yield()方法的线程告诉虚拟机它乐意让其他线程占用自己的位置。这表明该线程没有在做一些紧急的事情。注意,这仅是一个暗示,并不能保证不会产生任何影响。

在Thread.java中yield()定义如下:

1
2
3
4
5
6
7
/**
   * A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore
   * this hint. Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilize a CPU.
   * Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.
   */
 
public static native void yield();

让我们列举一下关于以上定义重要的几点:

  • Yield是一个静态的原生(native)方法
  • Yield告诉当前正在执行的线程把运行机会交给线程池中拥有相同优先级的线程。
  • Yield不能保证使得当前正在运行的线程迅速转换到可运行的状态
  • 它仅能使一个线程从运行状态转到可运行状态,而不是等待或阻塞状态

yield()方法使用示例

在下面的示例程序中,我随意的创建了名为生产者和消费者的两个线程。生产者设定为最小优先级,消费者设定为最高优先级。在Thread.yield()注释和非注释的情况下我将分别运行该程序。没有调用yield()方法时,虽然输出有时改变,但是通常消费者行先打印出来,然后事生产者。

调用yield()方法时,两个线程依次打印,然后将执行机会交给对方,一直这样进行下去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package test.core.threads;
 
public class YieldExample
{
    public static void main(String[] args)
    {
       Thread producer = new Producer();
       Thread consumer = new Consumer();
 
       producer.setPriority(Thread.MIN_PRIORITY); //Min Priority
       consumer.setPriority(Thread.MAX_PRIORITY); //Max Priority
 
       producer.start();
       consumer.start();
    }
}
 
class Producer extends Thread
{
    public void run()
    {
       for ( int i = 0 ; i < 5 ; i++)
       {
          System.out.println( "I am Producer : Produced Item " + i);
          Thread.yield();
       }
    }
}
 
class Consumer extends Thread
{
    public void run()
    {
       for ( int i = 0 ; i < 5 ; i++)
       {
          System.out.println( "I am Consumer : Consumed Item " + i);
          Thread.yield();
       }
    }
}
上述程序在没有调用yield()方法情况下的输出:
1
2
3
4
5
6
7
8
9
10
I am Consumer : Consumed Item 0
  I am Consumer : Consumed Item 1
  I am Consumer : Consumed Item 2
  I am Consumer : Consumed Item 3
  I am Consumer : Consumed Item 4
  I am Producer : Produced Item 0
  I am Producer : Produced Item 1
  I am Producer : Produced Item 2
  I am Producer : Produced Item 3
  I am Producer : Produced Item 4
上述程序在调用yield()方法情况下的输出:
1
2
3
4
5
6
7
8
9
10
I am Producer : Produced Item 0
  I am Consumer : Consumed Item 0
  I am Producer : Produced Item 1
  I am Consumer : Consumed Item 1
  I am Producer : Produced Item 2
  I am Consumer : Consumed Item 2
  I am Producer : Produced Item 3
  I am Consumer : Consumed Item 3
  I am Producer : Produced Item 4
  I am Consumer : Consumed Item 4

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值