-1

It is not to allow one thread at a time, but only one specific thread.

It is not a class I have access to (well I actually would like to avoid creating a fork), or I could use this, despite I think what jRat probably does could be used to make such approach automatic concerning classes we dont want to fork.

So, there is the main() thread where everything starts, I want to prevent all methods and field of all objects of an specific class to be modified by any other threads, by throwing exception/crash if that happens. Such objects must remain unmodified (it's field values) while the main thread works with it.

How would you do that? if it is really possible at all.

Community
  • 1
  • 1
Aquarius Power
  • 3,729
  • 5
  • 32
  • 67
  • 1
    You can look at `Thread.currentThread()` in your method and decide if you want to continue or not (based on thread name or object identity to a previously captured thread). – Thilo Sep 21 '16 at 04:17
  • 2
    But this does sound like an A-B-problem. What do you really need to achieve? – Thilo Sep 21 '16 at 04:18
  • 1
    For example, why not just make sure that no other thread gets a reference to the object (but only an immutable or otherwise restricted wrapper)? – Thilo Sep 21 '16 at 04:19
  • @Thilo If the object is modified (by another thread) while it should remain unmodified (it has some flags to detect it), the library will make the application crash. I have methods that can be called by any thread to modify these objects, and what you said really can help on that! – Aquarius Power Sep 21 '16 at 04:20
  • @Thilo yes that's good to, I could even use one identifier (not even a reference to the object) and queue modifications to be applied at the `main()` thread, interesting... :) – Aquarius Power Sep 21 '16 at 04:22
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, [describe the problem](http://meta.stackoverflow.com/questions/254393/what-exactly-is-a-recommendation-question) and what has been done so far to solve it. – xenteros Sep 21 '16 at 05:22
  • @xenteros, oops sorry, I am asking it if is possible to do what I described in some coding way; what I could I code, could it be some kind of class loader or something I dont know about java that could let me intercept all such class method calls to check for the right thread? Also, if there is something still obscure in what I described please specify it? I also need it for release, and not development environment. – Aquarius Power Sep 21 '16 at 20:13

1 Answers1

1

If you are trying to achieve this for some sort of debug/test purposes I suggest the following.

In Java, use AOP to intercept (by writing interceptor) all calls made to public/private functions of the library class (Spring examples - http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples). In the interceptor class, maintain information about thread (TID) accessing the intercepted methods. Now since your interceptor gets called before and after the library function, if a thread calls the library function while another thread is still executing library function, you can choose to throw exception.

For example -

1) you could maintain an atomic integer "tid" in the interceptor class which is set to 0 initially, and when a thread calls library function the interceptor gains control before the library function is called. 2) At this point try to compare and set the variable "tid". 3) After the library function is executed, again set this value to 0.

You can then throw exception if compare and set fails in step 2.

This solution will have some limitations depending on the object being intercepted.

Another way could be to write a wrapper over the library class in question and make it immutable.

Pranav
  • 66
  • 5
  • Cool, I was thinking about that. But you said like it is mainly useful in a development environment right? I wonder for release may be we shouldnt use it then? Also, do you believe a custom class loader could do the same but in release environment? And btw, despite I do not want to maintain a fork, I just thought may be a patcher script (like in bash + sed) that updates all methods of the single class to check for the proper thread could be a low hassle option too (I think a custom class loader could do the same?). – Aquarius Power Sep 21 '16 at 19:42
  • The solution I proposed is really a hack if you look at it, therefore I mentioned the caveat. There could also be limitations about using AOP. I don't fully understand the scope of your problem but in my opinion, to change the behaviour of any library class, it is better to write a wrapper around it that alters the behaviour of the library class in a clean way. Maintenance is also easier. Decorating a class as suitably is a widely used and accepted approach. Hope this helps. – Pranav Sep 22 '16 at 16:25