简体   繁体   中英

multiple threads accessing on a matrix

i' m implementing a multithreading program in java, where threads can enter in rows or columns concurrently.

example code, very little snippet to show my doubt:

public void checkRow(int row){

  for(int j = 0; j < numberOfColumns;i++){
        if(matrix[row][j]...)
  }

}

public void checkColumn(int column){
  for(int i = 0; i < numberOfRows;i++){
         if(matrix[i][column]...)
  }

}

and i have two separate thread loops

public void run(){

   matrixClass.checkRow(2);
}

public void run(){

   matrixClass.checkColumn(3);
}

my question is, i' m just reading the matrix so if threads for example reaches the same bucket something wrong will happen?

thanks in advance.

只要您正在读取矩阵,就可以了,尽管假设它已经安全地初始化了,并且不允许修改。

Immutable objects are Thread-safe. However there are additional pitfalls / cases where Thread sychronization may be needed

...if threads for example reaches the same bucket something wrong will happen?

Well, of course there is one cell at the intersection of the row and the column, and both threads must access that cell, so you must be asking, "what happens if they try to read the cell at the same time.

It's a fact of the way modern computer architectures are designed that we never have to say "at the same time." If two events E and F happen, then the three possibilities are; (1) we can prove that E happened before F, or (2) we can prove that F happened before E, or (3) we can't prove that either one happened before the other.

If the software and the hardware are both correctly designed, then the system will never get into a state where you can't prove which event happened first unless it doesn't matter which one happened first.

Suppose your two threads are running on two different CPUs. If at least one of those CPUs already has the value of the cell in its own local cache, then that CPU can read the value from cache while the other CPU gets the value from its cache or from memory. You won't be able to prove which happened first, but it won't matter because both threads will get the correct value.

If neither CPU has the value in its cache, then they'll both have to use the system bus to fetch the value from main memory. Only one CPU can use the bus at a time. A hardware designer knows how to prove that when there's a data race between two or more CPUs trying to use the bus, one of them will always win the race, and the others will be delayed. All of the CPUs eventually will get to use the bus, but the order in which they use it is unpredictable (unprovable).

A data race can be a problem in some programs, but if the program is correctly designed, then it won't matter.

In your case, the program is correctly designed as long as there is no other thread that you didn't tell us about that is writing to the cell. If everybody's only reading the cell, then everybody eventually will get the same answer even if you don't know/can't prove the order in which they read it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM