Re: How to run tasks with priority?
- From: "Boudewijn Dijkstra" <usenet@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 2 Jun 2005 18:48:27 +0200
"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.
.
- Follow-Ups:
- Re: How to run tasks with priority?
- From: George George via JavaKB.com
- Re: How to run tasks with priority?
- References:
- Re: How to run tasks with priority?
- From: George George via JavaKB.com
- Re: How to run tasks with priority?
- From: Boudewijn Dijkstra
- Re: How to run tasks with priority?
- From: George George via JavaKB.com
- Re: How to run tasks with priority?
- Prev by Date: Re: JVM Memory Limitations
- Next by Date: Simple XSL-FO date formatting question
- Previous by thread: Re: How to run tasks with priority?
- Next by thread: Re: How to run tasks with priority?
- Index(es):
Relevant Pages
|