Java - can we have a weak thread?

Can we have a weak link to the current thread, which should be interrupted at low processor performance?

I mean, something like this work?

WeakReference<Thread> ref = new WeakReference<Thread>(new Thread(){ public void run(){ while(true){ /* your code */ } } }); 

Thus, I want the thread to be executed with low priority, I mean that when the processor performance is low and the CPU is fully utilized, the thread stops automatically.

There are some threads that DO NOT have high priority and should be interrupted and terminated at low processor performance.

+5
source share
3 answers

This cannot be achieved through weak links in the way you describe. This article describes weak links well, so you might be wondering: weak link

There are two problems that you need to solve, easy and hard. I do not think there is anything in the standard Java API for this.

One easy thing is to complete the "low" priority:

An easy way to achieve the desired behavior would be to register Runnable instances with low priority, according to your application rules, with some kind of government manager. These instances implement the behavior of stop() and optionaly pause() and resume() and display it in the state manager, which can stop all instances on some kind of signal. You can just check out some volatile boolean in the run() loop for this.

Not so easy - determine CPU usage

Use JNI or some third-party library, such as one of them: How to control the use of computer, memory and disk in Java?

You can periodically check resource consumption using step 2, and if you determine that there is something suspicious, to notify the state manager of the shutdown of low priority tasks.

+3
source

Short answer: None . Sorry

There is no such mechanism in the standard API, and you cannot use WeakReference to create such a thing, because links refer only to the reachability of objects, and not to processor activity.

But this does not mean that the analogy is not neat, and this does not mean that this is a bad idea! I like it.

Hope you need to assign a low priority to the thread - but as noted elsewhere, this is subtle and quick anger. A low priority thread should give way to higher priorities and would be suitable for a while-loop thread. The dangers are that different OS scheduling systems can cause your low-priority thread to starve, especially if you are not too careful that higher priority threads are allocated from time to time. You can find wild variable behavior on multiprocessor processors and on individual processors.

Given that you want the task to actually stop when the processor loads ... you could use System.nanoTime() to execute each loop pass, and if it exceeds a threshold, then exit the while loop. I don’t like it very much: it is susceptible to GC pauses and natural changes leading to termination.

Alternatively, you can use JMX to measure the actual processor load, see How to get the percentage of OS processor utilization from java - and break the while loop if it exceeds the threshold value. You may have a monitoring thread that calls Thread.interrupt () for your worker to abort any blocking actions. I can imagine that you would spend your whole life setting it up - too impatient in some circumstances, too lethargic in others; wildly varying on different host sizes.

And if you want to kill threads while they actually work ... it starts very hard. I'm starting to think that there may be other approaches, but it's hard to say without knowing more about the wider problem you are trying to solve.

Still neat.

+2
source

To achieve your goal you need to use the priority of the thread .

The idea of ​​a weak link is to allow earlier garbage collection. But in the case of threads, it makes no sense to use it, since the thread itself is gc root and will not be garbage collected in the RUNNING state.

+1
source

All Articles