?
Solved

set priority for java.util.Timer

Posted on 2005-02-27
15
Medium Priority
?
1,283 Views
Last Modified: 2006-11-17
Hi,

 How do i set priority for the underlying thread associated to a Timer/TimerTask
0
Comment
Question by:zuzzi2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13417776
don't think you can
0
 
LVL 7

Accepted Solution

by:
tomboshell earned 150 total points
ID: 13417880
I don't think that goes with the concept of a timer.  A timer is based upon time and not priorities.  I would guess, but have never done this, that you could build a utils TimerTask with an instance of a Thread object (that you modify) since it implements the Runnable interface.  But I could not guarantee that it would work the way that you expect.

The best would be to simply make a high priority thread that contains a timer within it.  The thread container would then have a high priority, but then you will actually have two threads.  Or simply have your new thread calcuate the time delays itself (the old way that everyone did before Timers came with the JDK)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13417932
You can always try to write your own timer class and set the priority there :)
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 92

Expert Comment

by:objects
ID: 13418003
tomboshell's comment got me thinking, you should be able to access thread from any task using Thread.currentThread()
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13418032
I guess that timers use a back-ground thread of their own which sleeps for the specified duration (timer-interval) and then raises an event. That way, I don't think that Thread.currentThread () would give you access to 'that' Thread because it would be encapsulated within the Timer class. I could be wrong, though (this is only a guess).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13418037
However, in such cases - setting priority to a timer's thread does not make sense because all you want it to do is sleep.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 13418051
Yep, that will access the thread.  The only problem I ever had with that is when I tried to access that call within a class that is not the "owner" of the thread, ie it needs to be used from the calling class.  But, that said, I *may* have done something wrong in the past (that hurt).  On-the-fly (runtime changes) to the started thread can be tricky.  
0
 
LVL 92

Expert Comment

by:objects
ID: 13418053
> That way, I don't think that Thread.currentThread () would give you access to 'that'
> Thread because it would be encapsulated within the Timer class

Yes, but it runs the task on that thread :)


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13423661
It can't be done AFAIK. It's fully encapsulated, and even if you could break the encapsulation, you wouldn't be able to restart it with a new priority
0
 
LVL 92

Expert Comment

by:objects
ID: 13423897
> It can't be done AFAIK.

As I originally stated.
0
 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 150 total points
ID: 13425046
One of the things that makes java.util.Timer scalable is that it only has a single thread on which all TimerTasks execute, sequentially, according to the javadoc. It goes on to say, "Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes."

So it is doubtful that the designers wanted you to have a convenient mechanism to change thread priority -- particularly lower -- because then it would have the undesirable consequences mentioned above. And changing the thread priority to something higher would likely result in potential starvation of other threads when TimerTasks were executing.

However, if you really want to have a Timer and/or TimerTask that permits manipulation of thread priority you could easily scrounge the code out of the src.zip in your JDK implementation, change its packaging, and then make whatever changes you desired.

Best regards,
Jim Cakalic
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13427659
>>As I originally stated.

'Originally' being the operative word. You seemed to be stating something rather different later in the thread
0
 
LVL 92

Expert Comment

by:objects
ID: 13434403
I offered a workaround to try, that doesn't at all change what I originally stated.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13434474
>>Any reason to post your last comment?

I shall discuss that with you out of this thread if you wish
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question