Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java monitor include instance variables?

Tags:

Is it that monitor in Java does not restrict access to instance variables and only to the methods which are declared synchronized or code in synchronized statements?

I have created two threads, thread y invokes sync() method, which is declared synchronized while thread r invokes unsync() method which is not declared synchronized. Both invoke methods on shared object s.

Thread r is able to modify the instance variable of object s while the monitor or lock of that object is still being held by the thread y.

Is it that the monitor in Java does not restrict access to instance variables, and only to the methods which are declared synchronized or code in synchronized statements?

public class Stuff {

    private int a = 10;

    public synchronized void sync() {
        long t1 = System.currentTimeMillis();
        System.out.println("Okay, I am in sync() method. "
                        + "I will be waiting for 10 seconds. Current Time = "
                        + System.currentTimeMillis());
        while (System.currentTimeMillis() - t1 < 10000);
        System.out.println("Okay, I have waited for 10 seconds. Current time is "
                        + System.currentTimeMillis()
                        + ". Now I will exit from sync() method, a = " + this.a);
    }

    public void unsync() {
        System.out.println("Alright, I am in unsync() method. The current time is "
                        + System.currentTimeMillis());
        this.a = this.a + 1;
        System.out.println(". The time of exit from unsync() method is "
                        + System.currentTimeMillis());

    }
}

class T1 extends Thread {

    Stuff s;

    public T1(Stuff s) {
        this.s = s;
    }

    public void run() {
        s.sync();
    }
}

class T2 extends Thread {

    Stuff s;

    public T2(Stuff s) {
        this.s = s;
    }

    public void run() {
        s.unsync();
    }
}

class Main {

    public static void main(String args[]) throws Exception {
        Stuff s = new Stuff();
        T1 y = new T1(s);
        T2 r = new T2(s);
        y.start();
        Thread.sleep(2000);
        r.start();
    }
}

The output of the program is below:

 
Okay, I am in sync() method. I will be waiting for 10 seconds. Current Time = 1358801766310  
Alright, I am in unsync() method. The current time is 1358801768343. The time of exit from unsync() method is 1358801768343  
Okay, I have waited for 10 seconds. Current time is 1358801776310. Now I will exit from sync() method, a = 11
like image 216
Pushparaj Avatar asked Jan 21 '13 20:01

Pushparaj


People also ask

How does monitor work in Java?

Monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signaling other threads that their condition has been met. It is an entity that possesses both a lock and a wait set.

Where does instance variables are stored in Java?

Instance variables are declared in the class, but outside of the constructors, methods, or blocks of the particular class. They are used to represent the state of an object. Instance variables are stored in the heap section of the memory.

What kind of Java object is a monitor?

A monitor is simply a term for an object whose methods can be safely used in a multithreaded environment. If you scroll down, it's even got a section explicitly about Java. Show activity on this post. A monitor is like a building that contains one special room that can be occupied by only one thread at a time.

Is instance variable in Java?

What is instance variable in Java? Instance variables in Java are non-static variables which are defined in a class outside any method, constructor or a block. Each instantiated object of the class has a separate copy or instance of that variable. An instance variable belongs to a class.


2 Answers

Yes. Holding the monitor of an object prevents another thread from executing another block of code or synchronized on the same object. If a method is not synchronized, any thread can call it at any time, whether another thread holds a monitor or not.

Every access to a shared stated, even read-only accessed, must be synchronized if there's a chance that at least one thread modifies this shared state.

like image 127
JB Nizet Avatar answered Oct 08 '22 23:10

JB Nizet


Is it that monitor in java does not restrict access to instance variables and only to the methods which are declared synchronized or code in synchronized statements?

Yes.

Synchronized blocks (or methods) are, among other things, mutually exclusive. That does not prevent the object used as a lock (the monitor, let's call it lock) to be used outside those blocks, in which case no synchronization will be performed. For example, one thread could read or write lock while another thread is within a synchronized block where lock is the monitor.

If you want to restrict access to a variable, you need to make sure that all accesses are made while holding a lock (any lock, provided it is the same for each access).

like image 29
assylias Avatar answered Oct 09 '22 00:10

assylias