??????????????????????????????????????????????????????????к?????????????????????????????????(???????????У??????????????)?????????????????????????????á????????????????wait()??sleep()?????????????????Σ??????????join()??yield()?????????????????????????????ù??????κ????????????????????о??в????????????????????
????Java????????????
??????????????????У?Java??????????????????????????????????????????????ζ??Java?????е??????????????????????????????????Χ????????????????????????????????????????????????????????Java???????????????????
??????????????????????Java????????2?????????????????????????????и????????Java??????С??????????Java??????????????????????????????????????????????????????????ζ???????и??????????????????????????????????????????У??????ж?(???)????????????????????????????????????????ζ????????????????????????????????????????(??????????????????????????κ?????)
???????Java?????????????????????У??????????????????????????????????о??????????????????????????????????????????ζ???????и?????????????????????????????????У?????????????????????????????????????????????????????????????????Java?????
????????????????
????????????????????????????????????????????????????yield()??????????????
???????????????????????????????????Я????????????
????????????????1??10???Χ?????10????????????1????????????5????????????
?????????????????????????????????????????????????????????????????
?????????????е?????л?????????????????????е?????????????и???????????
??????????????????????????С?
????t.setPriority()?????趨???????????
??????????????????????????????????????????趨??
???????????ó???????MIN_PRIORITY??MAX_PRIORITY??NORM_PRIORITY???趨?????
?????????????????????????????????????????????????????
????yield()????
???????????yield??ζ????????????????????????yield()????????????????????????????????????????λ?á???????????????????Щ????????顣????????????????????????????????κ????
??????Thread.java??yield()???????£?
????/**
????* 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();
???????????о???1??????????????????
????Yield?????????????(native)????
????Yield????????????е????????л????????????????????????????
????Yield??????????????????е?????????????????е???
?????????????????????????????????????????????????????
????yield()??????????
???????????????????У?????????????????????????????????????????????趨?С??????????????趨????????????Thread.yield()???????????????????????иó?????е???yield()????????????????????????????????????????????????????????
????????yield()?????????????????δ?????????л?????????????????????????
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();
}
}
}