了解JVM会发生 - 之前和重新排序(Understanding JVM Happens-before and reorder)
我正在阅读关于内存模型的JLS规范, 17.4.5发生在订单之前 。 我不明白第一条规则:
“#如果x和y是同一个线程的动作,并且x按照程序顺序在y之前,那么hb(x,y)。”
我们假设A和B是可以在多个线程之间共享的对象(类对象的实例):
int i=A.getNum(); // ActionA int j=B.getNum(); // ActionB
三个问题:
根据上述规则,是否意味着hb(ActionA,ActionB) ?
如果对1的答案为真,那么这是否意味着根据规则之前发生的事情,ActionB无法在遵循JSR133内存模型的任何JVM的ActionA之前重新排序?
如果1和2都是真的,那么ActionA和ActionB似乎不相关,为什么不能重新排序呢? 只是为了这个规范?
I am reading the JLS spec on memory model, 17.4.5 Happens-before Order. I do not understand the first rule:
"# If x and y are actions of the same thread and x comes before y in program order, then hb(x, y)."
Let's assume A an B are objects (instances of class object) that can be shared between multiple threads:
int i=A.getNum(); // ActionA int j=B.getNum(); // ActionB
Three questions:
According to the above rule, does it mean hb(ActionA,ActionB)?
If the answer to 1 is true, does it mean according to happens-before rule, ActionB can not reordered to come before ActionA in any JVM that follows JSR133 memory model?
If 1 and 2 both are true, it seems that ActionA and ActionB are not relevant, why can not reorder them? just for this spec?
原文:https://stackoverflow.com/questions/6633983
最满意答案
你需要在这里的
accumarray
:[~,~,ia] = unique(A); [~,~,ib] = unique(B); M = accumarray([ia, ib], 1);
You'll need
accumarray
here :[~,~,ia] = unique(A); [~,~,ib] = unique(B); M = accumarray([ia, ib], 1);
相关问答
更多-
尝试这个: x = [ 0 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 1 1]; [ii, jj] = ndgrid(1:size(x,1)); y = arrayfun(@(m,n) find(x(m,:) & x(n,:)), ii, jj, 'uniformoutput', 0); 结果: y{1,1} = 2 3 5 y{2,1} = 2 3 y{3,1} = 3 y{4,1} = ...
-
%get t, removing all singleton dimensions t=squeeze(T(1,1,:)) %some operations t=t*rand(1) %writing t back T(1,1,:)=t %get t, removing all singleton dimensions t=squeeze(T(1,1,:)) %some operations t=t*rand(1) %writing t back T(1,1,:)=t
-
all()检查向量的元素是否都为零。 在matrizes上它返回第一个非单例维度的向量,所以你必须做两次。 for i=2:4 all( all(R2(1:i,1:i)) ) end all() checks if none of the elements of the vector are zero. On matrizes it returns the vector of hte first non-singleton dimension so you have to do it twice. ...
-
一种方法(使用M(:,1)选择第一列; M(:,1)<5返回第一列中最小于5的项的行索引)) - > R = M; > R(M(:,1)<5,1) = 0 R = 0 2 0 4 5 6 另一个 - R = M; [i,j]=find(M(:,1)<5); % locate rows (i) and cols (j) where M(:,1) < 5 % so j is just going to be all 1 ...
-
你需要在这里的accumarray : [~,~,ia] = unique(A); [~,~,ib] = unique(B); M = accumarray([ia, ib], 1); You'll need accumarray here : [~,~,ia] = unique(A); [~,~,ib] = unique(B); M = accumarray([ia, ib], 1);
-
你可以将它与角色'1'进行比较 >> A=['0101';'1011'] A = 0101 1011 >> A=='1' ans = 0 1 0 1 1 0 1 1 You can compare it with character '1' >> A=['0101';'1011'] A = 0101 1011 >> A=='1' ans = 0 1 0 1 1 0 1 1 ...
-
如果您希望删除基于1d列矩阵的2d矩阵中的列: output = train_data(:,find(toremove<1)); 如果是需要删除的行,则基于1d行矩阵: output = train_data(find(toremove<1),:); 如果我理解正确的话,可能会做这个工作。 If you wish to delete columns in 2d matrix based off 1d column matrix: output = train_data(:,find(toremove<1 ...
-
如果我理解正确,您希望根据第一 列的值对每个第三个暗淡切片中的 行进行排序 。 [m,n,p] = size(a); [~, row_ind] = sort(a(:,1,:), 1); lin_ind = bsxfun(@plus, bsxfun(@plus, row_ind, (0:n-1)*m), reshape((0:p-1)*m*n, 1, 1, p)); result = a(lin_ind); 这是如何工作的 : 沿第一维(行)排序a(:,1:,) ,并获得排序的索引(使用排序的第二个输出;第 ...
-
我认为问题主要是由于在分配期间隐式调整大小。 这就是为什么我认为: %# test parameters N = 5000; %# Size of 1 dimension of the square sparse L = rand(1,N) > 0.95; %# 5% of rows/cols will be non-zero values M = sum(L); rr = rand(M); %# the "data" to ...
-
您需要使用单元格数组并将单个元素引用为C{1} = C01 , C{2} = C02等。 您也可以将单元格数组创建为C = {C01, C02, C03}; 请注意,您仍然可以对单元格数组使用C(1)语法,但此语句将不返回原始数字矩阵,而是将矩阵封装到具有一个元素的单元格数组中。 You need to use cell array and refer individual elements as C{1} = C01, C{2} = C02, etc. You can also create the ce ...