The synchronous thread method runs simultaneously - why?

I have one thread question. I have a Thread class and 2 threads of objects are created.

public class MyThread extends Thread{

    String name="";

    public MyThread(String string) {
        name=string;
    }    

    @Override
    public void run() {
        callMe();
    }       

    synchronized private void callMe() {
        System.out.println("Started");
        for (int i = 1; i <= 5; i++) {
            System.out.println(name+" = "+i);
        }           
    }


    public static void main(String[] args) {            
        MyThread a = new MyThread("A");
        MyThread b = new MyThread("B");

        a.start();
        b.start();
    }       
}

When I do this, the output I get is

Started
Started
B = 1
B = 2
A = 1
A = 2
B = 3
A = 3
B = 4
A = 4
B = 5
A = 5

I know that A and B are printed randomly when the Thread Scheduler selects it.

But my question is: why is the loop NOT executing one after another? I used the keyword synchronized.

+4
source share
4 answers

Your method is synchronizedeffective:

private void callMe() {
    synchronized(this) {
        System.out.println("Started");
        for (int i = 1; i <= 5; i++) {
            System.out.println(name+" = "+i);
        }
    }
}

, this ... . , , :

public final class DemoRunnable implements Runnable {
    @Override
    public synchronized void run() {
        System.out.println("Started");
        for (int i = 1; i <= 5; i++) {
            System.out.println(Thread.currentThread().getName() + " = " + i);
        }
    }

    public static void main(String[] args) {
        Runnable runnable = new DemoRunnable();
        Thread a = new Thread(runnable, "A");
        Thread b = new Thread(runnable, "B");
        a.start();
        b.start();
    }
}

:

Started
A = 1
A = 2
A = 3
A = 4
A = 5
Started
B = 1
B = 2
B = 3
B = 4
B = 5

(, , ).

, ( DemoRunnable), , .

:

  • Runnable Thread;
  • Thread , Thread ; .
  • this - Object , ... , , ,
+13

. .

..

private final static Object globalLock = new Object();
// Later 
private void callMe() {
  synchronized (globalLock) {
    System.out.println("Started");
    for (int i = 1; i <= 5; i++) {
       System.out.println(name+" = "+i);
    }
  }
}
+9

 private void callMe(){
       synchronized(this){
           System.out.println("Started");
           for (int i = 1; i <= 5; i++) {
              System.out.println(name+" = "+i);
           }
       }                           
 }

. , , :

private void callMe(){
   synchronized(MyThread.class){
     System.out.println("Started");
     for (int i = 1; i <= 5; i++) {
          System.out.println(name+" = "+i);
     }
   }  
}                              
+3

2 2 .

Well, you should now get other answers. You create 2 separate objects, namely: a and b. Now, when you call the start method, a new thread is launched through the method of starting the corresponding (separate, not identical) objects. The synchronization is indeed in the method, but there is exactly one thread passing through the callMe a method and exactly one thread passing through the callMe method for b. Thus, you expect the result to be:

A - 1 A - 2 A - 3 ...

not happening.

Hope this helps

+2
source

All Articles