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

K

Kevin

(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!~
 
K

Kevin

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

Oliver Wong

Kevin said:
(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. :)

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
 
T

tom fredriksen

Kevin said:
(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.

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
 
K

Kevin

tom said:
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.
 
T

tom fredriksen

Kevin said:
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)?
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.

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

/tom
 
O

Oliver Wong

Kevin said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top