Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Background process only taking idle time, how to do it?

Thread view: 
Kevin - 21 Feb 2006 18:44 GMT
(I googled but did not find any answer)

In my program, there is a long time running thread that doing a lot of
IO and computation in background. I want to put this thread running
only when the PC's CPU is free (and hopefully the memory is "free"
too).

It seems that start that thread with Thread.MIN_PRIORITY is not what I
want. It forces the thread to give up CPU time if other programs
require the CPU, but the thread is running all the time AND we can
easily feel it. I am looking for a way to "sleep" the thread when other
programs are using the CPU.

Of course, CPU is in use by a lot of programs all the time.
The point is: using Thread.MIN_PRIORITY, we can still obviously feel
the coding is running there (especially if it involes a lot on IO,
compuation and memory). Some programs like google's desktop search,
user does not feel it is indexing the files (a lot of IO and
computation, I guess) beacuse it does so when the PC is "free". How to
catch that "free" in Java?

Hope I get my question clear. :-)

Nice day!~
Kevin - 21 Feb 2006 18:49 GMT
In another short way:

I want to "fool" the user that no code is running in the background.
:-D

I feel Thread.MIN_PRIORITY makes the thread low priority when it tries
to "compete" for the CPU and resource, it does not "give up
voluntarily".

Is that the case?
Oliver Wong - 21 Feb 2006 18:59 GMT
> (I googled but did not find any answer)
>
[quoted text clipped - 18 lines]
>
> Hope I get my question clear. :-)

   The short answer is that you probably want to lower the priority of your
process, rather than the priority of your thread; unfortunately, I don't
think you can do this in Java without resorting to JNI or native calls, and
thus making your program OS dependent.

   Read http://www.javaworld.com/jw-09-1998/jw-09-threads-p2.html 
especially the section titled "Get your priorities straight"

   - Oliver
tom fredriksen - 21 Feb 2006 20:11 GMT
> (I googled but did not find any answer)
>
[quoted text clipped - 8 lines]
> easily feel it. I am looking for a way to "sleep" the thread when other
> programs are using the CPU.

First problem is that to OS controls CPU scheduling, not the jvm. So you
can not do anything with the CPU scheduling without affecting the whole
program. The only thing you can affect through the jvm is the thread
priority. So unless you want to reduce the priority of the entire
program you have to figure out something with the code.

One obvious solution is to split the program in to two parts. Where the
main part is in one process and the heavy backend/batch part is another.
This allows you to reduce the priority of the backend part at a
process/OS level.

Another solution, which is halfway what you have already done, is to
give the backend thread a low priority while giving the other threads
the highest priority.

Still another solution is to program some sort of control logic for this
your self. You could for example, use yield(), or sleep(), in the
backend part after f.ex. each iteration or some small and divisible part
of the task. You could also implement a jvm scheduler your self.

Only the first solution give exactly what you are asking, but it might
not be feasible. So in short there are no easy answers on this one, you
need to decide on the best solution of the ones available to you.

/tom
Kevin - 21 Feb 2006 22:43 GMT
> One obvious solution is to split the program in to two parts. Where the
> main part is in one process and the heavy backend/batch part is another.
> This allows you to reduce the priority of the backend part at a
> process/OS level.

But we can not start a java program (JVM) with a different OS level
priority by using the java code, right? Unless we write a script to
call the java program with lower OS priority, as suggested from:
http://www.comptechdoc.org/os/windows/win2k/win2kprocess.html
(my code is for Win32)

One thing not sure: which method from below can get a piece of code to
run with lowest priority (for Wi32)?
1) start the java program as normal. But in the thread, using
Thread.min_priority.
2) start the java program (VM) using windows command as: start /low
"java myjava ...."
3) do the both two methods.
tom fredriksen - 21 Feb 2006 23:27 GMT
>> One obvious solution is to split the program in to two parts. Where the
>> main part is in one process and the heavy backend/batch part is another.
[quoted text clipped - 3 lines]
> But we can not start a java program (JVM) with a different OS level
> priority by using the java code, right?

I dont know about windows, but in linux a user is allowed to issue a
command one of two commands "nice LOWER_PRI CMD" or "renice LOWER_PRI PID"

Unless we write a script to
> call the java program with lower OS priority, as suggested from:
> http://www.comptechdoc.org/os/windows/win2k/win2kprocess.html
> (my code is for Win32)

But of course there is nothing wrong in dividing the program in a sort
of server/client model and allow them to communicate through TCP or some
IPC such as shared memory or pipes. And you can start it by a script.
Thats what I was thinking anyway. Of courese there is nothing wrong in
starting it from the main app, but I just find it a but awkward.

> One thing not sure: which method from below can get a piece of code to
> run with lowest priority (for Wi32)?
[quoted text clipped - 3 lines]
> "java myjava ...."
> 3) do the both two methods.

Dont know, its windows. But since windows supports threads natively, it
might not matter.

/tom
Oliver Wong - 22 Feb 2006 14:36 GMT
>> One obvious solution is to split the program in to two parts. Where the
>> main part is in one process and the heavy backend/batch part is another.
[quoted text clipped - 14 lines]
> "java myjava ...."
> 3) do the both two methods.

   Only 2. If you had read the article I'd posted, perhaps you'd understand
that.

<quote>
Read http://www.javaworld.com/jw-09-1998/jw-09-threads-p2.html
especially the section titled "Get your priorities straight"
</quote>

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.