[英]non abstract methods of abstract class thread safe
I have an abstract class with non abstract methods. 我有一个带有非抽象方法的抽象类。 I want to know if these non abstract methods are thread safe by default?
我想知道这些非抽象方法默认情况下是否是线程安全的吗?
To make the question more clear, here is an example 为了使问题更清楚,这里有一个例子
public abstract Class Animal {
private List foo;
non abstract method survive() {
//bla bla bla
foo++;
foo = foo * 2
foo = nul
//bla bla bla
}
abstract method eat();
abstract method sleep();
}
public class Cat extends Animal {
eat();
sleep();
}
public class Dog extends Animal {
eat();
sleep();
}
//etc etc
If both the Cat and Dog wants to survive at the same time, does Java takes care of it by default? 如果猫和狗都希望同时生存,那么Java是否会默认照顾它? Or should I make the Animal.survive() method as synchronized?
还是应该使Animal.survive()方法同步?
Abstract class methods are not any special in terms of concurrent access. 就并发访问而言,抽象类方法没有任何特殊之处。 If you are working on a static resource in your non-abstract method then you should make it synchronize to avoid any concurrent access problem.
如果您以非抽象方法处理静态资源,则应使其同步以避免任何并发访问问题。
If class A extends class B, then all the objects of class B can use public and non-abstract methods from A. Obviously, you can override this functions in B, but if you won't do this, then there would be no problem in using (public, non-abstract) functions from A. 如果类A扩展了类B,则类B的所有对象都可以使用A中的公共和非抽象方法。显然,您可以在B中重写此函数,但是如果您不这样做,那么就不会有问题使用A中的(公共,非抽象)函数
Word abstract in A put in front of a method means, that you have to override it in the subclass. 方法前面的A中的单词抽象意味着必须在子类中覆盖它。
好吧,根本没有问题,因为Dog和Cat是独立的对象->没有同步问题...
"Writing thread safe code is, at its core, about managing access to state, and in particular to shared, mutable state."
“编写线程安全代码的核心是管理对状态的访问,尤其是对共享的可变状态的访问。” - Java Concurrency In Practice -
-Java并发实践-
foo state is very important. foo状态非常重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.