Java?????yield??join??????????
???????????? ???????[ 2015/2/11 11:34:00 ] ????????Java ??????? ???
??????????????????????????????????????????????????????????к?????????????????????????????????(???????????У??????????????)?????????????????????????????á????????????????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();
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11