Home > Back-end >  Could you tell me how to modify as required, this
Could you tell me how to modify as required, this

Time:09-21

As shown in figure

CodePudding user response:

 
//thread 1
Public void the run () {
Synchronized (SyncThreads1. Class) {//to lock the same object, rather than the thread lock the object itself, because thread 1 and thread 2 object is not visible and do not affect each other, to lock the common resource will happen influence
X=y=0;
System. The out. Println (x);
}
}

//thread 2
Public void the run () {
Synchronized (SyncThreads1. Class) {
X=y=1;
System. The out. Println (x);
}
}

CodePudding user response:

reference 1st floor qybao response:
 
//thread 1
Public void the run () {
Synchronized (SyncThreads1. Class) {//to lock the same object, rather than the thread lock the object itself, because thread 1 and thread 2 object is not visible and do not affect each other, to lock the common resource will happen influence
X=y=0;
System. The out. Println (x);
}
}

//thread 2
Public void the run () {
Synchronized (SyncThreads1. Class) {
X=y=1;
System. The out. Println (x);
}
}

Thanks, but 10 threads how make

CodePudding user response:

Private static int x, y, I=1;


Public static void main (String [] args) {

for(; I<=10; I++) {
New Thread (new Runnable () {
@ Override
Public void the run () {
X=y=I;
System. The out. Println (y);
}

}).start();
}

}