Re: How to run tasks with priority?
- From: "Boudewijn Dijkstra" <usenet@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 9 Jun 2005 22:30:11 +0200
"George George via JavaKB.com" <forum@xxxxxxxxxx> schreef in bericht
news:1017ecd7413146159805617139672d49@xxxxxxxxxxxxx
> Thanks Boudewijn,
>
>
> Boudewijn Dijkstra wrote:
>>> Thanks Boudewijn,
>>>
>>[quoted text clipped - 10 lines]
>>> to verify that the difference behavior of yield method and sleep method.
>>> Do
>>> you have any good ideas?
>>
>>Make a thread that increments a variable as fast as it can. After every
>>increment it calls an abstract myWait() method that waits a number of
>>milliseconds. One implementation loops yield() until it reaches the wait
>>time. The other implementation simply calls sleep(long).
>>
>
> I am wondering whether your testing method will work. Since if there is
> only one thread in the system, yield will have no effect.
In this case "no effect" means that it will continue running, i.e. not
sleeping.
> But in your
> description, there are only one thread. Our testing purpose is to verify
> that yield method will idle CPU (if there are more than one threads running)
> , while sleep will not idle CPU.
>
> Maybe it is my mis-understanding of your description. It is highly
> appreciated if you could write down your ideas into sample source codes.
Agreed, but only because I became curious myself. ;)
Running the following test application should be accompanied by a CPU-usage
monitor program. First it will do a yield-test for 10 seconds, then it will
do a sleep-test for 10 seconds. Observe the difference in CPU usage. The
getFinalScore method doesn't really serve any purpose, besides maybe the
calculation of (TEST_TIME / WAIT_TIME).
public abstract class WaitTest
extends Thread
{
public static void main(String[] args)
{
WaitTest[] waitTest = new WaitTest[] {
new YieldWaitTest(),
new SleepWaitTest()
};
long TEST_TIME = 10000L;
try {
for (int i = 0; i < waitTest.length; i++)
{
WaitTest wt = waitTest[i];
System.out.print(wt.getTestName() + ": ");
wt.start();
Thread.sleep(TEST_TIME);
System.out.println(wt.getFinalScore());
}
} catch (InterruptedException ie) {
System.err.println("Interrupted");
System.exit(1);
}
}
static final int WAIT_TIME = 80;
boolean running = true;
int count = 0;
public void run()
{
int i = count;
while (running) {
try {
myWait();
} catch (InterruptedException ie) {
break;
}
i++;
}
count = i;
}
public int getFinalScore()
throws InterruptedException
{
running = false;
join();
return count;
}
// wait 80 ms
public abstract void myWait()
throws InterruptedException;
public abstract String getTestName();
}
class YieldWaitTest
extends WaitTest
{
public void myWait()
throws InterruptedException
{
long now = System.currentTimeMillis();
long target = now + WAIT_TIME;
while (true) {
Thread.yield();
if (target <= now)
break;
now = System.currentTimeMillis();
}
}
public String getTestName()
{
return "Yield";
}
}
class SleepWaitTest
extends WaitTest
{
public void myWait()
throws InterruptedException
{
Thread.sleep(WAIT_TIME);
}
public String getTestName()
{
return "Sleep";
}
}
.
- 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?
- From: Boudewijn Dijkstra
- 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?
- From: Boudewijn Dijkstra
- 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?
- 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: Arrays
- Next by Date: Remote Development
- Previous by thread: Re: How to run tasks with priority?
- Next by thread: Re: How to run tasks with priority?
- Index(es):
Relevant Pages
|