Synchronized keyword usage


Article: When a thread to access an object of a synchronized method or synchronized code block, the method synchronized or synchronized code block access to the object other threads are blocked.

public class MoreThread {
    public static void main(String[] args) {
         Piao piao = new Piao ();
         Thread t1 = new Thread(piao);
         Thread t2 = new Thread(piao);
         Thread t3 = new Thread(piao);
         t1.start();
         t2.start();
         t3.start();
    }
}

class Piao implements Runnable {
    @Override
    public void run() {
        /*synchronized(this) {*/
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName() + " 卖票:ticket " + i);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        /*}*/
    }
}

  Without adding synchronized:

Thread-1 ticket: ticket 0
Thread-0 ticket: ticket 0
Thread-2 Ticketing: ticket 0
Thread-1 ticket: ticket 1
Thread-0 ticket: ticket 1
Thread-2 Ticketing: ticket 1
Thread-1 ticket: ticket 2
Thread-0 ticket: ticket 2
Thread-2 Ticketing: ticket 2
Thread-1 ticket: ticket 3
Thread-0 ticket: ticket 3
Thread-2 Ticketing: ticket 3
Thread-0 ticket: ticket 4
Thread-2 Ticketing: ticket 4
Thread-1 ticket: ticket 4
Thread-2 Ticketing: ticket 5
Thread-0 ticket: ticket 5
Thread-1 ticket: ticket 5
Thread-1 ticket: ticket 6
Thread-2 Ticketing: ticket 6
Thread-0 ticket: ticket 6
Thread-1 ticket: ticket 7
Thread-0 ticket: ticket 7
Thread-2 Ticketing: ticket 7
Thread-1 ticket: ticket 8
Thread-0 ticket: ticket 8
Thread-2 Ticketing: ticket 8
Thread-1 ticket: ticket 9
Thread-2 Ticketing: ticket 9
Thread-0 ticket: ticket 9

  Plus synchronized: this code block to wait for one thread to access complete, another thread can access.

Thread-1 ticket: ticket 0
Thread-1 ticket: ticket 1
Thread-1 ticket: ticket 2
Thread-1 ticket: ticket 3
Thread-1 ticket: ticket 4
Thread-1 ticket: ticket 5
Thread-1 ticket: ticket 6
Thread-1 ticket: ticket 7
Thread-1 ticket: ticket 8
Thread-1 ticket: ticket 9
Thread-0 ticket: ticket 0
Thread-0 ticket: ticket 1
Thread-0 ticket: ticket 2
Thread-0 ticket: ticket 3
Thread-0 ticket: ticket 4
Thread-0 ticket: ticket 5
Thread-0 ticket: ticket 6
Thread-0 ticket: ticket 7
Thread-0 ticket: ticket 8
Thread-0 ticket: ticket 9
Thread-2 Ticketing: ticket 0
Thread-2 Ticketing: ticket 1
Thread-2 Ticketing: ticket 2
Thread-2 Ticketing: ticket 3
Thread-2 Ticketing: ticket 4
Thread-2 Ticketing: ticket 5
Thread-2 Ticketing: ticket 6
Thread-2 Ticketing: ticket 7
Thread-2 Ticketing: ticket 8
Thread-2 Ticketing: ticket 9

  


Article: When a thread to access an object of a synchronized method or synchronized code block, other threads can still access non-synchronized code block of the object.
Third: When a thread to access an object of a synchronized method or synchronized block of code, or other synchronized method synchronized code block access to the object other threads are blocked.

Guess you like

Origin www.cnblogs.com/huoyufei/p/11287576.html