在servlet或web服务中调用静态方法 [英] call static method in servlet or web service

查看:174
本文介绍了在servlet或web服务中调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在想一个问题。

我已经构建了一个JAVA类作为servle,我在其中有一个方法。

我想知道如果我将这个方法设置为静态方法会有什么不同。

我认为结果应该是相同的,但我想在这种情况下,在servlet和web服务的调用中知道tomcat或JRE是如何工作的。

Hi all,

I am thinking of a question.
I have built a JAVA class as a servle, inside which I have a method.
I am wondering what will be different if I set this method as a static one.
I think the result should be the same, but I want to know how tomcat or JRE works in this case, in both the call of servlets and web services.

推荐答案

会员11745740写道:
Member 11745740 wrote:



是的,我当然知道。 Servlet是多线程的,我想知道一个方法是否有静态属性很重要。


Yes, of course I know it. Servlet is multi-thread, I am wondering if a method has a static attribute matters.

好吧,关于你的我知道它,请看我对这个问题的评论。至于线程,让我们看看:



简单的分析可以告诉你使用静态方法添加多线程的问题,只能删除其中的一些。首先,静态方法是不使用this的方法,无法访问其类的实例。请查看我过去的答案以获得解释(无论是他们回答.NET还是C ++问题都没关系;原则完全相同):

c#中的类型转换 [ ^ ],

C#windows基于这个关键词及其在应用程序中的用途 [ ^ ],

Catch 22 - 当使用它们的函数变为静态时,指向接口对象的指针会死亡。 [ ^ ],

什么使静态方法可以访问? [ ^ ]。



当两个线程访问不同的对象时,线程不会相互干扰。如果仅使用静态方法并且仅将对象作为参数传递给它们,则可以轻松保证两个线程始终使用不同的对象。如果某些对象在这些线程之间共享,那么您可能遇到的大问题是您没有同步对它的访问。我希望你不必解释它。最好的同步是没有同步的。



所以,如果你在两个不同的线程中使用即时方法(非静态)方法,那一切都取决于它是什么这个对象。如果在同一个对象上调用同一个方法的不同线程中有两个调用,则使用相同的this,相同的对象,并且调用相同的方法并不重要:您仍然可以访问如果不同的线程在同一个实例上调用不同的实例方法,则该对象现在,这取决于你通过这个这个访问什么。所以,你的线程发生冲突,可能需要同步,比如锁定。



-SA




-SA

Well, about your "I know it" please see my comment to the question. And as to the threading, let's see:

Simple analysis could show you that using a static method add problems with multithreading, can only remove some of them. First of all, static method is the method not using "this", having no access to the instance of its class. Please see my past answers for explanations (it does not matter that they answered .NET, and of one the C++ questions; the principles are exactly the same):
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^],
What makes static methods accessible?[^].

When two thread access different objects, the threads don't interfere with each other. If you use only static methods and only the objects passed to them as parameters, you can easily guarantee that two threads always work with different object. If some object is shared between tho threads, you can big problem is you don't synchronize the access to it. I hope you don't have to explain it. And best synchronization is no synchronization.

So, if you use an instant method (not-static) method in two different threads, it all depends on what is that "this" object. If you have two calls in different threads to the same method called on the same object, you are working with the same "this", same object, and it does not even matter that you are calling the same method: you can still access the same object if different thread call different instance methods on the same instance. Now, it depends on what you access through this "this". So, your threads clash and may need synchronization, such as locking.

—SA


—SA


这篇关于在servlet或web服务中调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆