Re: How to run tasks with priority?



"George George via JavaKB.com" <forum@xxxxxxxxxx> schreef in bericht
news:66f2e92303ac409eade096696e2497a6@xxxxxxxxxxxxx
> Thanks Boudewijn,
>
>
> Boudewijn Dijkstra wrote:
>>> Thanks Boudewijn,
>>>
>>[quoted text clipped - 4 lines]
>>> which can flexibly define the number of sections in which a thread will be
>>> divided into.
>>
>>You don't actually have to define your code into sections. It should even
>>be >>possible to change the number of synchronized accesses dynamically, by
>>using
>> a loop inside your main loop:
>>
>>for (int i = syncCount; i >= 0; ) {
>> synchronized (lock) {
>> i--;
>> }
>>}
>
> Do you mean that when the current thread leaves a synchronized block, it
> will release the lock and then other threads will have chances to obtain
> the lock?

Yes. And if you've read a primer about Java synchronization, you wouldn't
have to ask that question. ;)

>>> Are there any practical uses that we should give away it's cycle to other
>>> threads with the same priority?
>>
>>Yes. You can use it in a lot of cases when you're using the sleep method,
>>but without the chance of actually idleing the CPU.
>
> Do you mean using yield method will idle the CPU?

No. I said that with yield, there wasn't a chance of actually idling the CPU
(as long as there are other threads ready).


.