1
package indi.JavaLearn;

public class MultiThread {
    private static int shared;
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            (new Thread() {
                public void run() {
                    write2();
                }
            }).start();
        }
    }
    public synchronized static void write2() {
        int i = 0;
        while (i < 2) {
            shared++;
            System.out.println("write," + Thread.currentThread().getId() + "," + String.valueOf(shared));
            i++;
        }
    }
}

Result

write,12,1
write,12,2
write,15,3
write,15,4
write,16,5
write,16,6
write,14,7
write,14,8
write,13,9
write,13,10
sinθ
  • 11,093
  • 25
  • 85
  • 121
  • 8
    Do you understand what `synchronized` does? – MadProgrammer Jan 05 '16 at 04:02
  • Can you elaborate on what the issue is? Edit: marking the method as synchronized will essentially make sure that the execution of that method from different threads will not overlap. If a call is made from a thread while another thread is still executing it, the second thread will wait until the first one is done executing. – Dimitre Bogdanov Jan 05 '16 at 04:24
  • There is no such thing as "single threaded mode" in Java. Furthermore you can see from your output that things are happening concurrently, because the threads are executing out of order. – dimo414 Jan 05 '16 at 04:41

3 Answers3

1

The synchronized modifier in write2 allows the method to only be run by one thread at a time - you need to remove it for it to be run on multiple threads at the same time.

If what you actually want to do is lock the shared variable, use a synchronized block:

synchronized(shared){
  shared++;
  System.out.println("write," + Thread.currentThread().getId() + "," + String.valueOf(shared));
}
1

public synchronized static void write2() is a static synchronized method implies lock/monitor is maintained at Class level instead of Object level.

static synchronized methods are synchronized on the Class object. If one thread is executing a static synchronized method, all other threads trying to execute any static synchronized methods will be blocked unless the lock is released by locked thread.

Non-static synchronized methods synchronize on this(means Object, which is an instance of the class). If one thread is executing a synchronized method, all other threads trying to execute any synchronized methods on that Object (but not the class) will be blocked.

Have a look at related SE questions:

Static versus non-static lock object in synchronized block

What is the difference between synchronized and static synchronized?

You can find good documentation about the concepts at this link

You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

You should read about concurrency in Java, specifically what the synchronized keyword means.

Now, on to what is going on with your code ...

When one thread (ex. the thread with the id 12) calls the write2() method it obtains a lock (mutex) on the object. Then it performs the code within the method body (i.e. the loop and print statement). Finally, it releases the lock on the object to allow another thread that is waiting to perform the method. Therefore, you always see the print statements occurring in segments of two.

RamV13
  • 547
  • 4
  • 8