5

Can we have a weak reference to a running thread to be terminated in low performance of CPU?

I mean, does something like this work?

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

In that way, I want a thread to have low priority to be executed, I mean when the CPU performance is LOW and CPU is engaged completely, the thread's execution be terminated automatically.

There are some threads which are NOT in high priority and should be interrupted and terminated in low CPU performance.

11
  • 1
    There is no relationship between weak references and thread priority...
    – assylias
    Aug 23, 2016 at 9:24
  • @assylias - so what should I do? Aug 23, 2016 at 9:24
  • 1
  • @assylias - I was aware of Thread Priority But I want it to be terminated even in RUNNING state Aug 23, 2016 at 9:32
  • 1
    Micro managing thread scheduling is going to be tricky in java. You could have a separate thread monitor CPU activity and interrupt your task if it becomes too high.
    – assylias
    Aug 23, 2016 at 9:37

3 Answers 3

3

This is not achievable via weak references in the way you describe. This article defines weak references nicely so you may be interested in this: weak reference

There are two problems you need to solve for this, easy and not so easy one. I don't think there is anything within standard Java API for this.

Easy one - terminate "low" priority threads:

Simple way to achieve desired behavior would be to register Runnable instances of a low priority, according to your application rules, to some kind of state manager. These instances would implement some kind of stop(), and optionaly pause() and resume() behavior and expose it to the state manager that could stop all the instances on some kind of signal. You could simply check some volatile boolean in run() loop for this.

Not so easy one - determine CPU usage

Either use JNI or some kind of third party library such as one of these: How to monitor the computer's cpu, memory, and disk usage in Java?

You could periodically check for resource consumption via step 2, and if you determine there is something fishy going on notify state manager to shutdown low priority tasks.

2

Short answer: No. Sorry!

There's no such mechanism in the standard API and you can't use WeakReference to build such a thing because references relate only to the reachability of objects, not to the CPU activity.

But that isn't to say that the analogy isn't neat, and it isn't to say it's a bad idea! I rather like it.

One hope you have is to assign a low Thread Priority - but as has been noted elsewhere, these are subtle and quick to anger. A low priority thread should give way to higher priority ones, and would be suitable for the while-loop thread. The dangers are that different OS scheduling systems may cause your low-priority thread to starve, especially if you aren't quite careful to make sure the higher priority threads block occasionally. You might find wildly variable behaviour on multiprocessors vs single processors.

Given that you want the task to actually terminate on CPU load ... you could use System.nanoTime() to time the execution of each pass of the loop, and if it exceeds some threshold, then quit the while loop. I don't like this much though: it's susceptible to GC pauses and natural variations causing the action to abort.

Alternatively, you could use JMX to measure the actual CPU load, see How to get percentage of CPU usage of OS from java - and abort the while-loop if it exceeds a threshold. You could have a monitoring thread that raises a Thread.interrupt() on your worker to abort any blocking activities. I can imagine you would spend a lifetime tweaking it though - too eager in some circumstances, too lethargic in others; wildly variable on different sized hosts.

And if you want to kill threads while they are actually running ... that starts getting really quite hard. I'm starting to think there might be other approaches, but that's hard to say without knowing more about the broader problem you are trying to solve.

Still neat though.

5
  • that's OK, no more way to implement? Aug 23, 2016 at 9:42
  • I'll add some thoughts to the answer. You'd be rolling your own mechanism though, and that might be a bit fraught with surprises.
    – SusanW
    Aug 23, 2016 at 9:44
  • @user3840019 ok, updated. Doesn't sound very easy, though (might be hard to make it stable), sorry.
    – SusanW
    Aug 23, 2016 at 10:12
  • I think I should give it up Aug 23, 2016 at 10:30
  • @user3840019 :-( ... but if you do find a solution, do comment here! I'm all interested. Maybe this is where the future Apache WeakThreads project is born...
    – SusanW
    Aug 23, 2016 at 10:37
1

You need to use thread priority to achieve your goal.

The idea of weak reference is to allow earlier garbage collection. But in case of threads there is no sense to use it since thread itself is gc root and will not be garbage collected in RUNNING state.

3
  • does JVM terminate threads in LOW_PERIORITY when some new Threads with HIGH_PRIORITY started?? Aug 23, 2016 at 9:35
  • @user3840019 any such behavior would be OS dependant. Most likely thread with low priority will get less CPU time than thread of higher priority, but it wouldn't be terminated.
    – John
    Aug 23, 2016 at 9:37
  • @user3840019 no. thread priority is about how many processor time will be allocated to each thread by os scheduler
    – vsminkov
    Aug 23, 2016 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.