java:如果我在每个请求上创建新对象,我还需要线程吗?

发布于 2024-11-09 16:59:25 字数 499 浏览 0 评论 0原文

一般问题也可以在 ci 中猜测
如果我有(在我的例子中 http request 类)从包装函数调用
这个包装函数是公共API。然后在包装函数内,我初始化新的 Request 对象,该对象假设使用来自包装函数的参数进行请求
我是否需要将请求对象包装在线程中(我有执行工作线程的线程池类)
为每个请求在堆栈上创建对象可以吗?
例如:

public void Wrapper(String a,String b)
{

// im doing ..
MyRequst req = new MyRequest(a,b); // will do the http requst 

}

or to do :

public void Wrapper(String a,String b)
{

// im doing ..
MyThreadPool.GetInstance().RunTask(new MyRequest(a,b)); // will do the http request 

}

general question it can be in c i guess also
if i have ( in my case http requst class ) that invoked from wrapper function
this wrapper function is public API . then inside the wrapper function i init new Request object that suppose to do request with the parameters coming from the wrapper function
do i need to wrap the request object in thread ( i have thread pool class that execute worker threads )
does creating object on the stack for each request will do ?
for example:

public void Wrapper(String a,String b)
{

// im doing ..
MyRequst req = new MyRequest(a,b); // will do the http requst 

}

or to do :

public void Wrapper(String a,String b)
{

// im doing ..
MyThreadPool.GetInstance().RunTask(new MyRequest(a,b)); // will do the http request 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-11-16 16:59:25

问题不是很清楚,但从可以推断的情况来看,相关问题是创建局部变量是否足以保证线程安全。答案是肯定的。

来自 Java 并发实践

访问共享的可变数据
需要使用同步;一种方式
避免这个要求就是不
分享。如果数据仅从
单线程,没有同步
需要。

应该记住,所有对象都存储在堆上。堆栈上的项目是 原语和对堆上的对象,被称为局部变量,并且始终是一个字宽(除了 long 和 double 值);不要将这些变量与 Java 编程语言中的方法局部变量的概念相混淆(人们错误地认为它存储在堆栈中)。

通过使用局部变量,可以确保堆上的对象只能由当前执行线程访问,当然,除非尝试与其他线程共享此类对象(在这种情况下,需要采用适当的同步技术) 。

The question isn't very clear, but from what can be inferred, the pertinent question is whether creating local variables is sufficient for thread-safety. The answer is yes.

From Java Concurrency in Practice:

Accessing shared, mutable data
requires using synchronization;one way
to avoid this requirement is to not
share. If data is only accessed from a
single thread, no synchronization is
needed.

It should be remembered that all objects are stored on the heap. The items on the stack are primitives and references to objects on the heap, and are termed as local variables and are always one-word wide (except for long and double values); these variables are not to be confused with the concept of method-local variables in the Java programming language (which people incorrectly assume to be stored on the stack).

By using local variables, one ensures that the objects on the heap are accessible only to the current thread of execution, unless of course, any attempt was made to share such objects with other threads (in which case appropriate synchronization techniques needs to be employed).

电影里的梦 2024-11-16 16:59:25

This gives an alternative to what you might be trying to do.
I am assuming you are trying to manipulate the request object. Have you considered using "HttpServletRequestWrapper".

http://download.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletRequestWrapper.html

Check this link: http://www.oracle.com/technetwork/java/filters-137243.html
In the above webpage goto the section which says "Programming Customized Requests and Responses".

Another example,
http://www.coderanch.com/t/172274/java-Web-Component-SCWCD/certification/When-HttpRequestWrapper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文