Java - multithreaded copy-on-write [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to add software enforced copy-on-write for multithreaded applications in Java? By this I mean threads having a reference to the same object, but when one thread attempts to modify it, the object pointed to is copied and the reference is adjusted to point to that copy.

The only implementation I know is the
java.util.concurrent.CopyOnWriteArrayList
see
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
and the related Set class
java.util.concurrent.CopyOnWriteArraySet
and finally
org.apache.mina.util.CopyOnWriteMap
but it depends from your need.

If your question is,
is it possible to enforce copy-on-write behavior across the board for an entire Java runtime
then the answer is,
No, there is no such general capability in Java.
Actually, I think the closest you can possibly get to that goal is using Clojure. All its default data structures are copy-on-write internally, and on the outside they are simply immutable objects.
The references you talk about are called, surprisingly, refs and they support full in-memory transactions. A simpler kind of a reference is atom, which fits your description 100%.
The whole Core API is devoted to elegant and epressive manipulation of these structures in a thread-safe, lock-free manner.

Yes. Lazy copying is easy to implement, but you would generally have to do it yourself.

Related

What exactly is a stream in java? And how are they related to Lambda-Expressions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I just don't get it. What are streams, how to use them and how to use Lambda expressions with it?
From the Javadoc:
A sequence of elements supporting sequential and parallel aggregate
operations
I think the below is key, however. The stream is making the elements of the collection available for the downstream operations.
A stream pipeline, like the "widgets" example above, can be viewed as
a query on the stream source. Unless the source was explicitly
designed for concurrent modification (such as a ConcurrentHashMap),
unpredictable or erroneous behavior may result from modifying the
stream source while it is being queried.
Note also:
Collections and streams, while bearing some superficial similarities,
have different goals. Collections are primarily concerned with the
efficient management of, and access to, their elements. By contrast,
streams do not provide a means to directly access or manipulate their
elements, and are instead concerned with declaratively describing
their source and the computational operations which will be performed
in aggregate on that source
If you're at all familiar with Scala (and noting its apparent absence of streams), it would be worth looking at this article too, which details the collection/stream differences, with particular focus on Java vs. Scala.

Java call performance vs search performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.

How to decide whether to use deep copy in Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm new to Java, and get really confused about deep copy.
I think each method that takes some mutable objects as arguments and returns an object that is related to the input should make defensive copy. However, after some coding I find this is very tricky.
For example, I want to choose some objects from a container satisfying some conditions, and returns them as a smaller container:
List<SomeType> chooseWithSomeCondition(List<SomeType> input)
But SomeType is defined by others, which is not immutable, not clonable, not serializable, neither has any copy constructor. Since Java's final cannot help either, I don't know how to make deep copy here.
Is my criteria too strict? Is the code I've read problematic? Are there some other ways to make deep copy? Please share your thoughts, and thanks in advance.
In this particular case, the code is problematic, and I'd just do a shallow copy. In your docs, note that the method is actually "chooseWithSomeConditionAtThisInstant" and tell others NOT to modify the elements of either list, or to do so with care and thought. (There are use cases where you want changes to come through.)
In the case of a type where you cannot use normal copying methods for deep copy (in your case of non-serializable, non-clonable, non-instantiable...), you would need to work around with reflection.
If you think reflection is too slow, or constructors can't be used, then you may want to think about using sun.misc.Unsafe to instantiate.

Precautions to Secure Java Coding [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am looking to gather some professional advises and precautions to do Secure Coding using Java. Couple of them I am already considering:
Should not log unwanted/excessive/sensitive information.
SQL injection should be taken care in parameterised queries, should use PreparedStatement or CallableStatement instead of Statement.
All the resources (db connections, input/output streams) should be release carefully.
Internal Exceptions should be caught and sanitized before propagating to upstream callers as it may reveal sensitive information.
Should clear the sensitive information even from memory when done as it can appear in core dump.
In case of inter-process communication, sensitive arguments should be encrypted.
Should use private for variables unless having good reason not to.
Should provide copy methods for sensitive classes.
Should prefer static factory method for object construction over public constructor.
Should avoid serialization of class that might hold sensitive information.
Appreciate any add up from the community.
Just another that comes to mind, you should use defensive copies when returning from a getter method.
If you're looking to protect your released code, you could consider something like JET although that's not quite what your question is asking I thought I'd just mention it.

what wrong with non object oriented approach to introduce object oriented approach [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i have been reading about this topic , and the more i read the more confused i get ,
can somebody please elaborate , we were using language C which follows structural approach ,
so what was wrong with this approach , that we moved to create a object oriented language JAVA .
I have been reading so many theoretical aspects , can some body please give more of a few practical illustrations ,
WHY WE NEEDED OBJECT ORIENTED APPROACH IN THE FIRST PLACE
I am not looking for an answer to be given in any interview or tutorial
I am looking for an answer to get the better understanding/practical importance of object oriented aproach
There are many explanations regarding this. But I would like to refer this
Modularity: The source code for a class can be written and maintained independently of the source code for other classes. Once
created, an object can be easily passed around inside the system.
Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from
the outside world.
Code re-use: If a class already exists, you can use objects from that class in your program. This allows programmers to
implement/test/debug complex, task-specific objects, which you can
then use in your own code.
Easy Debugging: If a particular object turns out to be a problem, you can simply remove it from your application and plug in a
different object as its replacement. This is analogous to fixing
mechanical problems in the real world. If a bolt breaks, you replace
it, not the entire machine.

Categories

Resources