java:如果我在每个请求上创建新对象,我还需要线程吗?
一般问题也可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不是很清楚,但从可以推断的情况来看,相关问题是创建局部变量是否足以保证线程安全。答案是肯定的。
来自 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:
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).
这为您可能尝试做的事情提供了替代方案。
我假设您正在尝试操纵请求对象。您是否考虑过使用“HttpServletRequestWrapper”。
http://download.oracle.com/javaee/1.3 /api/javax/servlet/http/HttpServletRequestWrapper.html
检查此链接:http://www.oracle.com/technetwork/java/filters-137243.html
在上面的网页中,转到“编程定制请求和响应”部分。
另一个例子,
http://www.coderanch.com/t /172274/java-Web-Component-SCWCD/certification/When-HttpRequestWrapper
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