Java thread synchronization failed( Understand whether the same object is locked or not)
Yilele 2021-06-04 11:21:18

java Thread synchronization synchronized invalid ???( Understand whether the same object is locked or not )

See if you lock the same object !

The situation is 1: Multiple threads execute the same object , That is, multiple threads running tasks execute the same task object

situation 2: Multiple threads execute different multiple objects , That is, multiple threads running tasks execute different multiple task objects

 

 

 

 

 

  Code :

*ts Test class

package eating;
public class ts {
public static void main(String[] args){
AppleNum appleNum = new AppleNum(20);
// Apple apple = new Apple(appleNum);
Apple[] apple = new Apple[5];
String[] names = { " Xiaohong ", " Xiao Huang ", " Small blue ", " Little green ", " indigo plant " };
for(int i = 0; i < names.length; i++) {
apple[i] = new Apple(appleNum);
new Thread(apple[i], names[i]).start();
}
}
}

*AppleNum Apple quantity category

package eating;
public class AppleNum {
/* The number of apples */
int num;
public AppleNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public int setNum(int num2) {
return num -= num2;
}
}

*Apple Apple

package eating;
import java.util.concurrent.TimeUnit;
class Apple implements Runnable {
private AppleNum appleNum;
public Apple(AppleNum appleNum) {
this.appleNum = appleNum;
}
@Override
public void run() {
while (true) {
synchronized (this) {
if (appleNum.getNum() > 0) {
appleNum.setNum(1);
System.out.println(Thread.currentThread().getName() + " I took the second " + appleNum.getNum() + " An apple ");
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
}
}
}
}
}
}

 

ps: Thread safety ? Non-thread safety ? What is thread synchronization ?

Thread safety ? Thread unsafe ?

Thread unsafe : When locking mechanism protection is not provided , There are multiple threads updating data successively, and the data obtained is dirty data . For example : When buying tickets , If you don't line up , Scrambling to buy tickets ,A See the number of votes is 100(B I saw it right away , stay A Before buying ,B The number of votes I saw was also 100),A bought , Stand on A The angle of , At this time, the number of votes is updated to 99, then B Come and buy , Stand on B The angle of , The number of votes should be 99( because B The number of votes you see is 100). Mingming was bought two tickets , It turned out to be 99.

What is thread synchronization ?~~ synchronized keyword 、 Thread lock

Thread synchronization : When threads access the same shared resource , Need to use thread synchronization mechanism , When the first thread goes in , Lock the shared resources , Then synchronize the state of the resource to the next thread . Thread synchronization is to pass the updated state of the previous thread to the next thread .

* Thread synchronization actually realizes Thread queuing . Prevent threads from accessing shared resources synchronously and causing conflicts . Variables need to be synchronized , Constants don't need to ( Constants are stored in the method area ).

 

 

 

Reference resources :

https://www.cnblogs.com/xrq730/p/6662232.html  

 

Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09