Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to run tasks with priority?
Hello everyone,


I want to run tasks with priority, i.e. among several simultaneous running
tasks, the task with the higher priority will have more chances to occupy
CPU time. I have the following 2 issues dealing with the implementation of
such feature.

- To define each task as a thread or as a method? How to change the
priority of each task dynamically when they are running?

- The priority feature of Java thread does not meet my requirement, since I
can not define priority precisely. For example, I want to define that a
task with priority A will occupy CPU time 3 times than a task with priority
B.

I am wondering whether I can find similar open source projects or tutorials?


Thanks in advance,
George

--
Message posted via http://www.webservertalk.com

Report this thread to moderator Post Follow-up to this message
Old Post
George George via webservertalk.com
05-29-05 01:58 PM


Re: How to run tasks with priority?
"George George via webservertalk.com" <forum@nospam.webservertalk.com> schreef in bericht
 news:a4ce638f7c5543e4bb1f3c3d5e711194@Ja
vaKB.com...
> Hello everyone,
>
>
> I want to run tasks with priority, i.e. among several simultaneous running
> tasks, the task with the higher priority will have more chances to occupy
> CPU time. I have the following 2 issues dealing with the implementation of
> such feature.
>
> - To define each task as a thread or as a method?

It would generally be wise to allocate a thread for each task.  How you defi
ne
the tasks, doesn't matter much.

> - How to change the
> priority of each task dynamically when they are running?

Thread#setPriority(int)

> - The priority feature of Java thread does not meet my requirement, since 
I
> can not define priority precisely. For example, I want to define that a
> task with priority A will occupy CPU time 3 times than a task with priorit
y
> B.

You can accomplish this behaviour with the Thread#yield() method.

> I am wondering whether I can find similar open source projects or tutorials?[/colo
r]

Similar to what?



Report this thread to moderator Post Follow-up to this message
Old Post
Boudewijn Dijkstra
05-31-05 09:03 PM


Re: How to run tasks with priority?
Thanks Boudewijn,

Your reply is very helpful! I found that if I use priority feature of Java
thread, then I can only have (MAX_PRIORITY - MIN_PRIORITY) different
priorities, which may limit my application. I am wondering whether there
are any alternate solutions which may have more different priorities.

> You can accomplish this behaviour with the Thread#yield
> () method.

How can I utilize utilize yield? Could you please give me a simple sample?


regards,
George

--
Message posted via http://www.webservertalk.com

Report this thread to moderator Post Follow-up to this message
Old Post
George George via webservertalk.com
06-01-05 08:58 AM


Re: How to run tasks with priority?
"George George via webservertalk.com" <forum@webservertalk.com> schreef in bericht
 news:00c78c327e394403907c1ce6b436fdd1@Ja
vaKB.com...
> Thanks Boudewijn,
>
> Your reply is very helpful! I found that if I use priority feature of Java
> thread, then I can only have (MAX_PRIORITY - MIN_PRIORITY) different
> priorities, which may limit my application. I am wondering whether there
> are any alternate solutions which may have more different priorities.

It sounds like you need to use synchronization, not priorities.  With
synchronized{} blocks you can make threads wait for eachother at fixed point
s.
 
>
> How can I utilize utilize yield? Could you please give me a simple sample?

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.



Report this thread to moderator Post Follow-up to this message
Old Post
Boudewijn Dijkstra
06-01-05 09:01 PM


Re: How to run tasks with priority?
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?

> 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?


regards,
George

--
Message posted via webservertalk.com
http://www.webservertalk.com/Uwe/Forums.as...-setup/200506/1

Report this thread to moderator Post Follow-up to this message
Old Post
George George via webservertalk.com
06-02-05 09:01 AM


Re: How to run tasks with priority?
"George George via webservertalk.com" <forum@webservertalk.com> schreef in bericht
 news:b41eddf5e0834dcea8e19f19bfafde21@Ja
vaKB.com...
> Thanks Boudewijn,
>
> 
>
> 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 th
e
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);
}
}
}
}
 
>
> 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 chanc
e
> 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.



Report this thread to moderator Post Follow-up to this message
Old Post
Boudewijn Dijkstra
06-02-05 08:59 PM


Re: How to run tasks with priority?
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.

> 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.

Are there any practical uses that we should give away it's cycle to other
threads with the same priority? Should we do it in this approach (yield)
manually or simply let JVM to do this.


Have a nice wend,
George

--
Message posted via webservertalk.com
http://www.webservertalk.com/Uwe/Forums.as...-setup/200506/1

Report this thread to moderator Post Follow-up to this message
Old Post
George George via webservertalk.com
06-03-05 08:58 AM


Re: How to run tasks with priority?
"George George via webservertalk.com" <forum@webservertalk.com> schreef in bericht
 news:1b0ed44698de4e4fb399a8187c1e13e0@Ja
vaKB.com...
> 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 Task
1
> is divided into 3 sections. I am wondering whether there are any approache
s
> 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 b
e
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, b
ut
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.  ;)



Report this thread to moderator Post Follow-up to this message
Old Post
Boudewijn Dijkstra
06-03-05 09:00 PM


Re: How to run tasks with priority?
Thanks Boudewijn,


Boudewijn Dijkstra wrote: 
>[quoted text clipped - 4 lines] 
>
>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 usin
g a
>loop inside your main loop:
>
>for (int i = syncCount; i >= 0; ) {
> synchronized (lock) {
>  i--;
> }
>}

Do you mean that when the current thread leaves a synchronized block, it
will release the lock and then other threads will have chances to obtain
the lock?
 
>
>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.

Do you mean using yield method will idle the CPU? If it is true, do you
know how to write a simple program to test that sleep method will not idle
CPU, and at the same time yield method will idle CPU?


Have a nice wend,
George

--
Message posted via webservertalk.com
http://www.webservertalk.com/Uwe/Forums.as...-setup/200506/1

Report this thread to moderator Post Follow-up to this message
Old Post
George George via webservertalk.com
06-04-05 08:57 PM


Re: How to run tasks with priority?
"George George via webservertalk.com" <forum@webservertalk.com> schreef in bericht
 news:66f2e92303ac409eade096696e2497a6@Ja
vaKB.com...
> Thanks Boudewijn,
>
>
> Boudewijn Dijkstra wrote: 
>
> Do you mean that when the current thread leaves a synchronized block, it
> will release the lock and then other threads will have chances to obtain
> the lock?

Yes.  And if you've read a primer about Java synchronization, you wouldn't
have to ask that question.  ;)
 
>
> Do you mean using yield method will idle the CPU?

No.  I said that with yield, there wasn't a chance of actually idling the CP
U
(as long as there are other threads ready).



Report this thread to moderator Post Follow-up to this message
Old Post
Boudewijn Dijkstra
06-05-05 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:39 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.