Re: How to run tasks with priority?



"George George via JavaKB.com" <forum@xxxxxxxxxx> schreef in bericht
news:b41eddf5e0834dcea8e19f19bfafde21@xxxxxxxxxxxxx
> Thanks Boudewijn,
>
>
>> It sounds like you need to use synchronization, not
>> priorities. With synchronized{} blocks you can make
>> threads wait for eachother at fixed points.
>
> It is a very good idea! I am not quite familar with the technology you
> mentioned. Could you please provide me a simple sample?

Upon entry of a synchronized block, the thread waits to acquire a lock on the
specified object. Only one thread can hold the same lock at the same time.
Upon exit of the synchronized block, the lock is released. At this point,
other threads waiting to acquire the lock, may acquire the lock and then
continue execution. In the following (untested) example, Task1 will execute 3
times as slow as Task2.

class LockHolder {
private static final Object lock = new Object();
public static Object getLock() {
return lock;
}
}
class Task1 implements Runnable {
private static final Object lock = LockHolder.getLock();
public void run() {
while (true) {
synchronized (lock) {
stuff(1);
}
synchronized (lock) {
stuff(2);
}
synchronized (lock) {
stuff(3);
}
}
}
}
class Task2 implements Runnable {
private static final Object lock = LockHolder.getLock();
public void run() {
while (true) {
synchronized (lock) {
stuff(1);
stuff(2);
stuff(3);
}
}
}
}

>> If two threads have the same priority and execute the
>> same code, adding a yield call to one of those, could
>> make it run about twice as slow.
>
> I am wondering why should I use yield to make one thread run slower than
> another one. I think thread with equal priority should have the same chance
> to run. What is your purpose of using yield?

To make the thread give away it's cycle to other threads with the same
priority. In other words, to reduce the execution time without reducing the
priority.


.



Relevant Pages

  • Re: Why cant I set the priority of softirq-hrt? (Re: 2.6.17-rt1)
    ... High prio task is blocked on lock and boosts 3 other tasks. ... I'll try to see what I can do though, but I am afraid the patch might get too big. ... A boosts B whichs is blocked interruptible on a lock a boosts C, ... A wakes up first a decrease the priority of B but not C, because the B runs on another CPU, but with lower priority and fixes C. ...
    (Linux-Kernel)
  • Re: Multi Threading Options
    ... If the avatars struct could be modified by any other thread, ... your modifications are so short that releasing the main lock and locking just ... Given that the refresh is timer=based, you could probably eliminate all synchronization ... If it is a UI thread, then while the drawing is happening, the PostMessage ...
    (microsoft.public.vc.mfc)
  • [PATCH RT RFC v2 8/8] rtmutex: pi-boost locks as late as possible
    ... real-time lock that supports priority inheritance. ... We check the priority of the owner relative to the waiter on ...
    (Linux-Kernel)
  • [PATCH RT RFC v4 8/8] rtmutex: pi-boost locks as late as possible
    ... real-time lock that supports priority inheritance. ... We check the priority of the owner relative to the waiter on ...
    (Linux-Kernel)
  • Re: threading - Monitor.Wait/Pulse
    ... thread synchronization is considered a "Bad Thing" since there is no way ... WaitOne/WaitAny/WaitAllmethods of the WaitHandle indicates whether ... don't get with WaitHandles is that it releases the Lock on the Object ... RendezvousDemo demo = new RendezvousDemo; ...
    (microsoft.public.dotnet.general)