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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
user3840019
  • 155
  • 1
  • 10
  • 1
    There is no relationship between weak references and thread priority... – assylias Aug 23 '16 at 09:24
  • @assylias - so what should I do? – user3840019 Aug 23 '16 at 09:24
  • 1
    http://stackoverflow.com/questions/20333150/java-multithreading-thread-priority – assylias Aug 23 '16 at 09:25
  • @assylias - I was aware of Thread Priority But I want it to be terminated even in RUNNING state – user3840019 Aug 23 '16 at 09: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 '16 at 09:37
  • @assylias - as I'm aware, there is no way to interrupt your threads execution from outside the thread, how can it be possible? – user3840019 Aug 23 '16 at 09:38
  • [Threads can be interrupted with `thread.interrupt()`](https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html) - and in your task you need to check if you have been interrupted, in which case you can stop whatever you are doing and let the thread die. You should maybe read about how concurrency works in Java. – assylias Aug 23 '16 at 09:41
  • @assylias - I know thread.interrupt() only changes a variable to true and you yourself have to check wheather to break your statement or no – user3840019 Aug 23 '16 at 09:44
  • @user3840019 It also produces an interrupted exception for many operations like `Thread.sleep` – vsminkov Aug 23 '16 at 09:45
  • @user3840019 I have a question. Why do you need to handmade thread controlling while you can delegate it to OS? Why do you think that thread priorities will not feet your needs? – vsminkov Aug 23 '16 at 09:47
  • cause some tasks have no high priority as their execution time will last more than a normal time and there are some threads which have high priority and have to be executed any time triggered and terminate though non-sence threads – user3840019 Aug 23 '16 at 10:00

3 Answers3

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.

Community
  • 1
  • 1
John
  • 5,189
  • 2
  • 38
  • 62
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.

Community
  • 1
  • 1
SusanW
  • 1,550
  • 1
  • 12
  • 22
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.

vsminkov
  • 10,912
  • 2
  • 38
  • 50
  • does JVM terminate threads in LOW_PERIORITY when some new Threads with HIGH_PRIORITY started?? – user3840019 Aug 23 '16 at 09: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 '16 at 09:37
  • @user3840019 no. thread priority is about how many processor time will be allocated to each thread by os scheduler – vsminkov Aug 23 '16 at 09:38