1. Is the way to use synchronized here correct?
2. Do the randomScore is locked when one thread access to it so that other threads can not access to randomScore?
3. If only changeRandomScore() can access to randomScore variable and only one thread can access to changeRandomScore(), therefore only one thread can acccess to randomScore at a time. Is it 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); } } }