Java 同步化块doens';不要阻挡不同的物体
我在hashMap(每行)中插入了以下内容: 在一些线程调用此函数之后:Java 同步化块doens';不要阻挡不同的物体,java,multithreading,Java,Multithreading,我在hashMap(每行)中插入了以下内容: 在一些线程调用此函数之后: @Override public void run() { while (true) { increaseRow(new Random().nextInt(10)); try { sleep(3000); } catch (InterruptedException e) {
@Override
public void run() {
while (true) {
increaseRow(new Random().nextInt(10));
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void increaseRow(Integer row) {
synchronized (rows.get(row)) {
for (int j = 0; j < column; j++) {
matrix[row][j] += 1;
}
System.out.println("begin print");
for (int i = 0; i < this.row; i++) {
System.out.println();
for (int j = 0; j < column; j++)
System.out.print(matrix[i][j]);
}
System.out.println();
System.out.println("end print");
}
}
贴图只有10个键值,我正在同步对象值,所以我不明白出了什么问题
提前感谢。由于您同步了同一行上的访问,打印会被打乱。因此,两个不同行的更新可能会交错。这可能没问题,只是印刷看起来乱七八糟 要同步打印(有多行可以并行更新,但只有一个屏幕),请执行以下操作:
public void increaseRow(Integer row) {
synchronized (rows.get(row)) {
for (int j = 0; j < column; j++) {
matrix[row][j] += 1;
}
// Printing here makes sense for debugging only
synchronized (rows) (
System.out.println("begin print");
for (int i = 0; i < this.row; i++) {
System.out.println();
for (int j = 0; j < column; j++)
System.out.print(matrix[i][j]);
}
System.out.println();
System.out.println("end print");
}
}
}
public void increaseRow(整数行){
已同步(行。获取(行)){
对于(int j=0;j
顺便说一句:为什么要对行锁使用哈希映射而不仅仅是数组?要强制它只打印“开始打印”然后“结束打印”,您需要在“行”对象上同步每次行更新(不是交错) 虽然一次只有一个线程可以写入rows.get(0)对象,但您仍然可以同时锁定rows.get(0)和rows.get(1)以及rows.get(2)对象。这意味着您的输出可以在一行中多次包含“开始打印”,然后是多个“结束打印”行 根据您在这里尝试实现的目标,我只需输出您正在更新的行,因为您知道可以同时完成两个操作,只同步您想要的行
public void increaseRow(Integer row) {
synchronized (rows.get(row)) {
for (int j = 0; j < column; j++) {
matrix[row][j] += 1;
}
System.out.println(String.format("begin update row %s", row));
for (int i = 0; i < this.row; i++) {
System.out.println();
for (int j = 0; j < column; j++)
System.out.print(String.format("row %s value %s",row,matrix[i][j]));
}
System.out.println();
System.out.println("String.format("end update row %s", row)");
}
public void increaseRow(整数行){
已同步(行。获取(行)){
对于(int j=0;j
}
或者,如果您只想让每个线程告诉您它更新了哪一行:
public void increaseRow(Integer row) {
synchronized (rows.get(row)) {
for (int j = 0; j < column; j++) {
matrix[row][j] += 1;
System.out.print(String.format("row %s value %s",row,matrix[row][j]));
}
}
}
public void increaseRow(整数行){
已同步(行。获取(行)){
对于(int j=0;j
我不明白。问题是什么?问题是为什么打印没有同步?同步工作基于每个实际对象,而不是值。也就是说,它工作于==
而不是.equals
,尽管这可能会与自动装箱有点混淆。所以如果你有foo!=bar
但foo.equals(bar)
,然后synchronized(foo){…}
和synchronized(bar){…}
不会相互影响。它们可能是在不同的对象上同步的。@SotiriosDelimanolis我不明白。我不能在同一行上同步,因为例如,如果一个线程将行1的锁与线程1同步,另一个想要行1的线程就不能,这不是问题。更新的同步是可以的,但是如果您想要t为了避免打印重叠,您需要在更全局的东西上同步。谢谢您的代码片段,您能给我解释一下地图上的嵌套锁吗?嗯,每次您更新行(x)时,这会在行上强制执行一个块-基本上,当你同时更新每一行时,你是在强迫你的线程在打印时互相等待。下面我的答案是最好的,它允许所有行同时更新,并同时打印到控制台,除了对同一行的同时更新。好的,谢谢你的解释。因此e线程一直在等待synchronized(rows)
,直到它们可以打印。OP似乎希望所有打印都作为一个批处理进行。我认为这不会修复任何问题,除非同步打印,在这种情况下,您将返回到JVM synchronizeprintln(*)的“全局”锁定(如果不是全部的话)实现函数,至少从Java 1.1左右开始-无需重新同步,只有在写入控制台的实际操作期间才有全局锁。
public void increaseRow(Integer row) {
synchronized (rows.get(row)) {
for (int j = 0; j < column; j++) {
matrix[row][j] += 1;
}
System.out.println(String.format("begin update row %s", row));
for (int i = 0; i < this.row; i++) {
System.out.println();
for (int j = 0; j < column; j++)
System.out.print(String.format("row %s value %s",row,matrix[i][j]));
}
System.out.println();
System.out.println("String.format("end update row %s", row)");
}
public void increaseRow(Integer row) {
synchronized (rows.get(row)) {
for (int j = 0; j < column; j++) {
matrix[row][j] += 1;
System.out.print(String.format("row %s value %s",row,matrix[row][j]));
}
}
}