gpt4 book ai didi

java - Java 中的功能等价

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:11:17 25 4
gpt4 key购买 nike

我正在阅读 Effective Java,并遇到了 Joshua Bloch 推荐的情况

class MyComparator extends Comparator<String>{
private MyComparator(){}
private static final MyComparator INSTANCE = new MyComparator();

public int compare(String s1,String s2){
// Omitted
}
}

XYZComparator is stateless, it has no fields. hence all instances of the class are functionally equivalent. Thus it should be a singleton to save on unnecessary object creation.

那么,如果它没有字段,则创建它所指向的任何类的静态最终对象 总是安全吗?当从两个线程并行调用 compare 时,这不会导致多线程问题吗?或者我误解了一些基本的东西。如果没有共享字段,是否每个线程都具有执行自主权?

最佳答案

So is it always safe to create a static final Object of whatever class it is pointing to if it has no fields?

我敢说是。没有字段会使类无状态,因此是不可变的,这在多线程环境中总是可取的。

无状态对象始终是线程安全的。

不可变对象(immutable对象)始终是线程安全的。

摘自 Java 并发实践:

Since the actions of a thread accessing a stateless object cannot affect the correctness of operations in other threads, stateless objects are thread-safe.

Stateless objects are always thread-safe.

The fact that most servlets can be implemented with no state greatly reduces the burden of making servlets threadͲ safe. It is only when servlets want to remember things from one request to another that the thread-safety requirement becomes an issue.

...

An immutable object is one whose state cannot be changed after construction. Immutable objects are inherently thread-safe; their invariants are established by the constructor, and if their state cannot be changed, these invariants always hold.

Immutable objects are always thread-safe.

Immutable objects are simple. They can only be in one state, which is carefully controlled by the constructor. One of the most difficult elements of program design is reasoning about the possible states of complex objects. Reasoning about the state of immutable objects, on the other hand, is trivial.


Wouldn't this cause multithreading issue when compare is called from two threads parallelly?

没有。每个线程都有自己的堆栈,其中存储局部变量(包括方法参数)。线程的栈不是共享的,所以没有办法并行地搞砸它。

另一个很好的例子是无状态 servlet。摘自那本好书。

@ThreadSafe
public class StatelessFactorizer implements Servlet {
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = factor(i);
encodeIntoResponse(resp, factors);
}
}

StatelessFactorizer is, like most servlets, stateless: it has no fields and references no fields from other classes. The transient state for a particular computation exists solely in local variables that are stored on the thread's stack and are accessible only to the executing thread. One thread accessing a StatelessFactorizer cannot influence the result of another thread accessing the same StatelessFactorizer; because the two threads do not share state, it is as if they were accessing different instances.


Is it like every thread has autonomy of execution if no fields is shared?

每个线程都有自己的程序计数器、堆栈和局部变量。有一个术语“线程限制”,它的一种形式称为“堆栈限制”。

Stack confinement is a special case of thread confinement in which an object can only be reached through local variables. Just as encapsulation can make it easier to preserve invariants, local variables can make it easier to confine objects to a thread. Local variables are intrinsically confined to the executing thread; they exist on the executing thread's stack, which is not accessible to other threads.

阅读:

关于java - Java 中的功能等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57900884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com