1. La façon d'utiliser le terme "synchronisé" est-elle correcte ?
2. Est-ce que le score aléatoire est verrouillé lorsqu'un thread y accède, de sorte que les autres threads ne peuvent pas accéder au score aléatoire ?
3. Si seul changeRandomScore() peut accéder à la variable randomScore et qu'un seul thread peut accéder à changeRandomScore(), alors un seul thread peut accéder à randomScore à la fois. Est-ce correct ?
import java.*;
public class StudentThread extends Thread {
int ID;
public static int randomScore;
StudentThread(int i) {
ID = i;
}
public void run() {
changeRandomScore();
System.out.println("in run");
}
public synchronized void changeRandomScore() {
randomScore = (int) (Math.random()*1000);
}
public static void main(String args[]) throws Exception {
for (int i = 1;i< 10 ;i++)
{
StudentThread student = new StudentThread(5);
student.start();
Thread.sleep(100);
System.out.println(randomScore);
}
}
}