java中同一个实例如何在另一个类的不同方法上工作
基于提供的参数,在多个方法中使用了相同的实例。我们可以在这里考虑线程安全吗? 假设我们有2堂课, class main& 类演示。
Class Demo {
returnType methodExample(args...) {
//based on the argument provide it returns
return something
}
}
Class Main {
Demo demo = new Demo();
returnType method1() {
value = demo.methodExample(args);
}
returnType method2() {
value = demo.methodExample(args);
}
}
它将共享相同的 demo 实例。两种方法的效果都不同步。 还请描述春季注射案件,以@Autowired的注释
道歉,对错别字或其他错误表示歉意。
Same instance is used in multiple method based on the argument providing. can we consider thread safety here.
Let's assume that we've 2 class,
Class Main &
Class demo.
Class Demo {
returnType methodExample(args...) {
//based on the argument provide it returns
return something
}
}
Class Main {
Demo demo = new Demo();
returnType method1() {
value = demo.methodExample(args);
}
returnType method2() {
value = demo.methodExample(args);
}
}
will it share same Demo Instance. And what of both method works asynchronously.
please also describe the case in Spring Injecting by @AutoWired annotation
Apologies for typos or other mistakes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它将共享相同的演示实例。但是计算是根据方法methodexample()进行的,是基于您对参数传递的值。
首先使用@Autowire,您必须在演示类上使用@Component注释。
yes it will share same Demo instance. but the calculations are performed under method methodExample() is based on what value you are passed on parameters.
for using @autowire first you have to use @component annotation on Demo Class.