Re: How to run tasks with priority?



"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";
}
}


.



Relevant Pages

  • Re: [PATCH] memcg: remove trylock_page_cgroup
    ... I used yield() before cond_reschedbut I was told don't use it. ... shorter intervals if the platform implements hi-res timers. ... And an attempt to sleep for 1us will fall back to 1/HZ if the platform ... when the loop finds a page is busy. ...
    (Linux-Kernel)
  • Re: Seeking C# references about Ruby Code block functionality
    ... public void each ... Haven't used yield myself yet so YMMV. ... An array of instructions which passed the test are returned, ... // or a zero-length array. ...
    (comp.lang.ruby)
  • Re: lock
    ... I dont think yield is no more supported in winos, or that it calls to sleep 0. ... But I spent so much time testing that I took a break from it, so dont take my word for it just yet. ... Spesifically scary was that it worked for the real app, but not for the test app case. ...
    (alt.lang.asm)
  • Re: [PATCH] memcg: remove trylock_page_cgroup
    ... I used yield() before cond_reschedbut I was told don't use it. ... shorter intervals if the platform implements hi-res timers. ... And an attempt to sleep for 1us will fall back to 1/HZ if the platform ...
    (Linux-Kernel)
  • Re: Net::Telnet
    ... After adding the sleep everything works well. ... It sounds to me like either c or the regexp are modified by a different ... and what cmd is supposed to yield), it's difficult to guess any more... ...
    (comp.lang.ruby)