简体   繁体   中英

Java Synchronized Method on Static Variable

I'm doing a college class on Java concurrency and was recently given a simple task to create 5 threads numbered from 1 to 5, then get each thread to write its thread number to a static variable in the class using a synchronized static method.

The solution I was given by the lecturer is below:

public class thread1 extends Thread {

private int threadNumber;
private static int threadCount = 0;
private static int value;

public thread1(){
      threadNumber = ++threadCount;
      System.out.println("Thread " + threadNumber + " is created" );
}

public synchronized static void setValue(int v){
      value = v;
      try{
           Thread.currentThread().sleep(100);
      }
           catch(InterruptedException e ){}

      System.out.println("the current value of the variable is " + value);
}

public void run() {
    setValue(threadNumber);
    return;
}

public static void main(String[] args) {
    for(int i = 0; i < 5; i++){
        thread1 thr = new thread1();
        thr.start();
}

}
}   

The output is supposed to be as follows:

Thread 1 is created
Thread 2 is created
Thread 3 is created
Thread 4 is created
Thread 5 is created
the current value of the variable is 1
the current value of the variable is 2
the current value of the variable is 3
the current value of the variable is 4
the current value of the variable is 5

But the output I am getting is below:

Thread 1 is created
Thread 2 is created
Thread 3 is created
Thread 4 is created
Thread 5 is created
the current value of the variable is 1
the current value of the variable is 5
the current value of the variable is 4
the current value of the variable is 3
the current value of the variable is 2

The order of current value is obviously different each time.

Is the solution I've been given incorrect? It's obviously not working in fulfilling its intended purpose which is to print out each of the thread's variables in order. 变量。

Could anyone shed some light on how I'd get it to print the thread numbers in order from 1 to 5 each time reliably? I thought the use of the synchronized method would do the trick, but obviously not. 方法可以解决问题,但显然不能。

The Solution provided by your lecturer is absolutely correct.

You are not getting the intended order because 5 different Threads are created and each of them have to access the same method which is Synchronized.

When a method is Synchronized only One Object at a time can access it

In your case Only One Thread can access the setValue method ,

when one Thread enters the method it acquires the lock and 
the other threads wait for the lock ,when this Thread releases the lock , 
any waiting Thread can acquire it and execute the setValue method.

You can never guarantee in which order Threads will execute the method ,

so you will get some different order of Threads on running this program eachtime

There is no guarantee that threads will execute in the order you call start method on them. A synchronized block only means that two threads cannot access it simultaneously. The order of the thread to run is determined by scheduler.

The order in which the thread enters the monitor is not defined. Once a thread finishes execution inside the monitor any other thread waiting on that monitor can gain control of the critical section.

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