Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Nhưng, những gì sẽ xảy ra nếu đề tài phát hành khóa đầu đọc và sau đó cố gắng để reacquire khóa đọc trước khi nhà văn đã có một cơ hội để có được các khóa? Nếu đề tài được phép reacquire khóa, sau đó bất kỳ chủ đề chờ đợi trong hàng đợi nhà văn có khả năng có thể thiếu bất cứ lúc nào với khóa. | 364 CHAPTER 15 CANONICAL FORMS two integers the hash values is executed along with the function calls to acquire them. If the call to Equals is expensive then this optimization will return some gains on a lot of the comparisons. If the call to Equals is not expensive then this technique could add overhead and make the code less efficient. It s best to apply the old adage which states that premature optimization is poor optimization and apply such an optimization after a profiler has pointed you in this direction and if you re sure it will help. Object.GetHashCode exists because the developers of the standard library felt it would be convenient to be able to use any object as a key to a hash table. The fact is not all objects are good candidates for hash keys. Usually it s best to use immutable types as hash keys. A good example of an immutable type is System.String. Once created you cannot ever change it. Therefore GetHashCode on a string instance is guaranteed to always return the same value for the same string instance. It becomes more difficult to generate hash codes for objects that are mutable. In those cases it s best to base your GetHashCode implementation on calculations performed on immutable fields inside the mutable object. For the sake of example suppose you want to implement GetHashCode for a ComplexNumber type. One solution is to compute the hash based on the magnitude of the complex number as in the following example Imports System Public NotInheritable Class ComplexNumber Private ReadOnly real As Double Private ReadOnly imaginary As Double Other methods removed for clarity Public Sub New ByVal real As Double ByVal imaginary As Double Me.real real Me.imaginary imaginary End Sub Public Overrides Function Equals ByVal other As Object As Boolean Dim result As Boolean False Dim that As ComplexNumber TryCast other ComplexNumber If Not that Is Nothing Then result Me.real that.real AndAlso Me.imaginary that.imaginary End If Return result End Function Public