首页 \ 问答 \ (Servlets,线程安全)保护会话和上下文状态((Servlets, Thread safety) Protecting the session and context state)

(Servlets,线程安全)保护会话和上下文状态((Servlets, Thread safety) Protecting the session and context state)

参考:Head First Servlets&JSP,第204页,第三个问题:

问:“实现SingleThreadModel的效果与同步服务方法几乎相同。两者都可以在不保护会话和上下文状态的情况下使Web应用程序瘫痪。”

这意味着什么:“在不保护会话和上下文状态的情况下,将Web应用程序放在膝盖上”? 他们是否说实施SingleThreadModel是不够线程安全的? 因为即使一次只有一个线程可以在一个servlet上运行,这也不会阻止其他servlet访问和改变上下文/会话作用域变量? 如果Servlet实现SingleThreadingModel,那么为什么需要保护会话状态? 它一次只允许一个线程。 即使您打开一个新的浏览器窗口,我认为servlet也不允许您发出两个请求。 也许他们的意思是,如果不同的Servlet在这个状态下不同步,那么一次只有一个线程的Servlet仍然可能破坏上下文/会话状态?

为什么类(静态)变量不是线程安全的? 因为所有线程都可以访问和修改(损坏)该值?

它对我来说仍然有点模糊。


Referring to: Head First Servlets & JSP, page 204, third question:

Q. "The effect of implementing SingleThreadModel is virtually the same as synchronizing the service method. Both can bring a web app to its knees without protecting the session and context state."

What does this mean: "bring a web app to its knees without protecting the session and context state"? Are they saying that it's not enough thread-safe to implement the SingleThreadModel? Because even though only one thread at a time can be running at one servlet, this doesn't stop other servlets from accessing and mutating the context/session scoped variables? If a Servlet implements the SingleThreadingModel, then why would you need to protect session state? It only allows one thread at a time. Even if you open up a new browser window, the servlet wouldn't allow you to make two requests, I think. Maybe they mean that different Servlets with one thread at a time could still corrupt the context/session state if they don't synchronize on that state?

And why are class (static) variables not thread-safe? Because all threads can access and modify (corrupt) that value?

It's still a little vague to me.


原文:https://stackoverflow.com/questions/17806517
更新时间:2023-07-27 12:07

最满意答案

第一个内环将是1 + 2 + 4 + 8 .. 2 ^ M,其中2 ^ M <= N * N.

2到N * N的幂的总和约为2 * N * N或O(N ^ 2)

注意:当N = 100000时,N * N将溢出,因此其结果具有误导性。 如果你认为溢出是问题的一部分,对于大数,总和是相当随机的,所以你可以争论它的O(1),即如果N = 2 ^ 15,N ^ 2 = 2 ^ 30并且总和将是整数.MAX_VALUE。 没有更高的N值会给出更高的总和。


The first inner loop will be 1 + 2 + 4 + 8 .. 2^M where 2^M is <= N * N.

The sum of powers of 2 up to N * N is approximately 2 * N * N or O(N ^ 2)

Note: When N=100000 , N*N will overflow so its result is misleading. If you consider overflow to be part of the problem, the sum is pretty random for large numbers so you could argue its O(1), i.e. if N=2^15, N^2 = 2^30 and the sum will be Integer.MAX_VALUE. No higher value of N will give a higher sum.

相关问答

更多
  • 正如@emory所指出的那样,自动确定任意一段代码的大时间复杂度(证明是从停机问题中减少)是不可能的。 然而,有些工具可以尝试通过在多个不同的输入上运行来测量一段代码的复杂性。 Goldsmith,Aiken和Wilkerson在论文“衡量经验计算复杂性”中描述了一种这样的工具。 它通过尝试对程序的运行时间与其输入大小进行回归来实现。 这个名为trend-prof的工具可以在线获得。 希望这可以帮助! As @emory pointed out, it is provably impossible to d ...
  • Java中的ArrayList是由array支持的List 。 get(index)方法是一个常数时间, O(1) ,操作。 代码直接出自ArrayList.get(index)的Java库: public E get(int index) { RangeCheck(index); return (E) elementData[index]; } 基本上,它只是返回一个值直接从后面的数组。 ( RangeCheck(index) )也是恒定时间) An ArrayList in Java ...
  • 新答案 从Java 7生命周期的更新6开始, substring的行为更改为创建一个副本 - 所以每个String指向一个不与任何其他对象共享的char[] ,据我所知。 所以在这一点上, substring()成为一个O(n)操作,其中n是子串中的数字。 老答案:Java前7 没有文件 - 但在实践中O(1)如果你不认为垃圾收集是必需的等等 它只是构建一个引用相同的底层char[]但是具有不同的偏移量和计数值的新的String对象。 所以成本是执行验证所需的时间,并构造一个新的(相当小的)对象。 这就是O ...
  • 复杂性是函数 (或算法)的渐近属性,而不是实际执行路径的渐近属性。 复杂性表示输入大小和计算时间(或空间要求)之间的关系。 因此,当应用于单个具体计算时,该概念没有意义。 换句话说,你可以询问计算f(n)的复杂性取决于n ,而不是计算f(5) 。 后者只是一个数字。 前者是一个功能。 你可以做的是计算实际的操作次数。 每次执行要包含的操作(例如“比较”)时,只需递增一些全局计数器,然后检查其值。 (算法的复杂性应该告诉你这个计数器可能采用的值的界限。) Complexity is an asymptotic ...
  • 第一个内环将是1 + 2 + 4 + 8 .. 2 ^ M,其中2 ^ M <= N * N. 2到N * N的幂的总和约为2 * N * N或O(N ^ 2) 注意:当N = 100000时,N * N将溢出,因此其结果具有误导性。 如果你认为溢出是问题的一部分,对于大数,总和是相当随机的,所以你可以争论它的O(1),即如果N = 2 ^ 15,N ^ 2 = 2 ^ 30并且总和将是整数.MAX_VALUE。 没有更高的N值会给出更高的总和。 The first inner loop will be 1 ...
  • public static String reverse(String s) { String rev = " "; for (int i=s.length()-1; i>=0; i--) rev.append(s.charAt(i); // <--------- This is O(n) Return rev.toString(); } 我复制粘贴你的代码。 我不确定你在哪里得到这个,但实际上String没有append方法。 也许rev是一个StringBuilder或另一个Appen ...
  • 设n是字符串中的字符数。 你的循环显然迭代n次(因为text.length()== n),每次迭代都做不变的工作(加法)。 你的循环应该是O(n) 编辑:其他答案是错误的。 您没有返回字符串,也没有附加到StringBuilder。 您正在添加每个ASCII字符的int值,并返回总计。 Let n be the number of characters in your string. Your loop obviously iterates n times (since text.length() == n ...
  • 最坏的情况是O(n^2)如果你假设所有的块都是“自由的”并且它们的大小减小为2,最后两个为1: 2^n, 2^(n-1) ... , 64, 32, 16, 8, 4, 2, 1, 1 第一次迭代合并后两次,触发一个递归调用,该调用现在在一个看起来像的列表上运行 2^n, 2^(n-1) ... , 64, 32, 16, 8, 4, 2, 2 合并最后两个,递归调用,现在运行 2^n, 2^(n-1) ... , 64, 32, 16, 8, 4, 4 第一次n次循环,然后是n-1,n-2,.... ...
  • 从它的外观来看,我认为编写此代码的人必须知道这些问题的答案。 无论如何,Priority Queue基于Max Heap实现。 所以,复杂性如下: 第35行 - O(log k)在堆中插入元素的时间。 在插入时堆中遵循自下而上的方法。 第37行 - O(1) ,检查堆大小的时间,通常是与堆一起维护的。 第39行 - O(log k) ,删除堆头的时间,堆根部的堆化方法应用于删除堆的顶部。 第35-40行 :从上述复杂性我们可以看出,一次迭代的总体复杂度将为O(log k) 。 此循环针对n元素运行,因此总体 ...
  • 假设您没有其他数据知识并且必须使用通用排序方法,那么最佳理论就地排序算法是O(n * log(n))。 Arrays.sort方法应该使用其中之一,并且是没有更多信息的最佳选择。 如果您愿意使用大量内存,则可以使用非就地排序,例如基数或计数。 这些可以比n * log(n)更快,有些和O(n)一样快,但可能使用O(n)或更差的内存。 如果您知道具有特殊属性的数据(例如它几乎已经被排序),则插入排序或类似算法可能比O(n * log(n))更快而不使用内存,但没有更多信息,其中一个可以不建议。 Assumin ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。