• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to run tasks with priority?

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt you can do this since these precise aspects of the JVMs operation are not defined by the specification.
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stuart,


Originally posted by Stuart Gray:
I doubt you can do this since these precise aspects of the JVMs operation are not defined by the specification.



Sorry that I do not express my ideas very clearly. In my mind, I do not want to control the processing time very precisely, and I just want to make the processing time conforming with priority approximately. Do you have any ideas?


regards,
George
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I think it is still dependant on the implementation of the JVM. There is no to guarantee a specific priority or order of execution AFAIK.
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stuart,


Originally posted by Stuart Gray:
No, I think it is still dependant on the implementation of the JVM. There is no to guarantee a specific priority or order of execution AFAIK.



Your reply is very helpful to guide me to make the decision, i.e. whether I will use Java to implement this feature. I want to know whether your opinions are Java is not suitable for this feature.


regards,
George
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by George Lin:
Thanks Stuart,

Your reply is very helpful to guide me to make the decision, i.e. whether I will use Java to implement this feature. I want to know whether your opinions are Java is not suitable for this feature.

regards,
George



The reason that the Java specification does not strictly enforce how the priority will affect the timeslice of the thread is because most JVMs are written to use the underlying threading model.

However, the threading model on Windows will try to give more time to a higher priority thread, while making sure that the low priority thread do not starve. So if you are using Java on Windows, it will behave somewhat close to what you want. (Be warned, there are many other factors that affect what happens to a thread)

Henry
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Henry confirms, I think 'try' is the closest you are going to get when it comes to thread management.

Question: Will the thread management vary even across different versions of Windows (I would imagine so, at least because 9x only supports single processors, while the NT kernel supports multiple processors).
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-How to change the priority of each task dynamically when they are running?

You may want to experiment with the setPriority() method to see what it does. Here is an example:




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


Does the CPU time need to be sliced precisely? If you could live with just minimum and maximum priorities, the threading model may work for you. Otherwise, perhaps you need to consider some other solution. What's the problem you are trying to address?
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John,


Originally posted by John Smith:
-How to change the priority of each task dynamically when they are running?

You may want to experiment with the setPriority() method to see what it does. Here is an example:




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


Does the CPU time need to be sliced precisely? If you could live with just minimum and maximum priorities, the threading model may work for you. Otherwise, perhaps you need to consider some other solution. What's the problem you are trying to address?



Your reply is very great! 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.

The problem I am meeting with is to develop a simple QoS prototype, in which different tasks can run according to user defined priorities.


regards,
George
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


If you look at the implementation of the java.lang.Thread class, you will see that the priority is a simple int and the range is defined between 1 and 10:



So, yes, you can call setPriority and pass any int from 1 to 10. However, don't get too excited. As other posters noted, there are no guarantees that the underlying OS will comply with your code. For one thing, consider this: Solaris supports 2^31 priority levels, and Windows NT supports only 7. When you specify one of the Java's 10 priorities, they will be mapped into one of the OS's priorities, and this mapping is undefined. Your best bet is to limit your choices to Thread.MAX_PRIORITY, Thread.MIN_PRIORITY, and Thread.NORM_PRIORITY, but even then the scheduling will vary from one OS to another.

The best way to think about Java thread priorities is as a mere hint to the underlying OS scheduler. As such, if your task is to prioritize the tasks in a reliable, platform-independent way, most people would probably advise you against using Java priorities.

Let me throw you something alternative to you: how about giving up on threads and priorities, and making use of a priority queue which contains the tasks to be executed in a sequence? That way, you can rearrange the tasks dynamically, at any time, in a very precise fashion, your code will be portable, and the results will be predictable.

You may also consider two celebrated books on Java thread programming:
Java Thread Programming by Paul Hyde and Concurrent Programming in Java by Doug Lea
[ May 31, 2005: Message edited by: John Smith ]
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,


Originally posted by Henry Wong:


The reason that the Java specification does not strictly enforce how the priority will affect the timeslice of the thread is because most JVMs are written to use the underlying threading model.

However, the threading model on Windows will try to give more time to a higher priority thread, while making sure that the low priority thread do not starve. So if you are using Java on Windows, it will behave somewhat close to what you want. (Be warned, there are many other factors that affect what happens to a thread)

Henry



Your reply is very helpful! Do you mean using setPriority of Java Thread? If so, could you please recommend some good tutorials dealing with how to use priority feature of Java thread efficiently? I do not have much experience on this feature.

Another issue is that, it seems Windows and Linux use different thread models in your reply, could you please explain it in more details or provide some online resources?


regards,
George
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stuart,


Originally posted by Stuart Gray:
As Henry confirms, I think 'try' is the closest you are going to get when it comes to thread management.

Question: Will the thread management vary even across different versions of Windows (I would imagine so, at least because 9x only supports single processors, while the NT kernel supports multiple processors).



Your reply is very helpful. I am wondering where can I find related resources (open source projects, tutorials or papers) dealing with different thread models on different platforms, just as you have mentioned.


regards,
George
 
George Lin
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John,


Originally posted by John Smith:

Let me throw you something alternative to you: how about giving up on threads and priorities, and making use of a priority queue which contains the tasks to be executed in a sequence? That way, you can rearrange the tasks dynamically, at any time, in a very precise fashion, your code will be portable, and the results will be predictable.



Your idea of using priority based queue is great!! I think you are an expert of this topic. I do not have very much exporience about this topic before, could you provide me a simple sample? Are there any online resources (open source projects, tutorials or papers) dealing with this topic?


regards,
George
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic