Re: How to run tasks with priority?



In article <42a08662$0$1335$5fc3050@xxxxxxxxxxxxxxxxxxxxxxxx>,
Boudewijn Dijkstra <usenet@xxxxxxxxxxxxxxxxxxx> wrote:
>"George George via JavaKB.com" <forum@xxxxxxxxxx> schreef in bericht
>news:1b0ed44698de4e4fb399a8187c1e13e0@xxxxxxxxxxxxx
>> Thanks Boudewijn,
>>
>>
>> Your sample is very helpful. But I think there is an issue in your sample,
>> which is that we must hardcode the number of sections that a code block
>> will be divided into. For example, in your sample, you hardcoded that Task1
>> is divided into 3 sections. I am wondering whether there are any approaches
>> 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--;
> }
>}
>
>> 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.
>
>> Should we do it in this approach (yield)
>> manually or simply let JVM to do this.
>
>That is a decision that I cannot make for you. ;)

Doesn't it depend to some extent on whether the JVM implementation
tries to do timeslicing if there's more than one thread with
the same priority? As far as I know, some do and some don't.
Then again, it's probably best to write in a way that will work for
any standard-conforming implementation ....

(And -- clever idea. My initial reaction was that it wasn't going to
be possible to do what the OP wanted without a lot more knowledge of
exactly how long various parts of his code took and/or of how the
JVM schedules threads. But after thinking more -- yeah, I think
what you're proposing might actually work, though I wonder whether
there isn't some strange way a JVM could do scheduling that would
defeat your scheme.)

--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.
.



Relevant Pages

  • Cooperative threading preemptive threading - a bit confused
    ... then in multithreaded program JVM ... Cooperative threading model ... threads with higher priority can preempt lover priority ... Is JVM always the one that manages thread movement (by thread ...
    (comp.lang.java.programmer)
  • Re: Massive performance loss from OS::sleep hack
    ... We may just want to add some sort of interface so the ... If the JVM kept track of the thread priorities in use, ... Thread.yieldcould first lower the current thread's priority to ... this argument and the Java developers won :-(. ...
    (freebsd-performance)
  • Re: Hashtables
    ... I think the claim is that, in any JVM implementation in ... the address changes but the hash code ... Did anyone here claim the hashCode is always the same as its current ... In a handle-style JVM it COULD be since the handle slot number never ...
    (comp.lang.java.help)
  • Re: [OT] Is loading the second Java application faster than loading the first?
    ... again for every Java application. ... OS and JVM implementation, of course). ... This will also apply to any DLLs loaded via JNI. ...
    (comp.lang.java.programmer)
  • Re: Richard heathfields C programming article
    ... For a start I'm saying that, contrary to what you may think, there is more than one implementation of javac and more than one implementation of the Java Virtual Machine. ... Based on my exposure, though I didn't spend much time on the core JVM, and as I recall:- ... As I've already pointed out - the Jalapeno JVM implementation is written in Java. ... I'm aware of one JVM where the core bytecode interpreter was implemented in assembler. ...
    (comp.lang.c)