Synchronization block (method) of Java multi-thread synchronization mechanism-synchronized and multi-thread synchronized

Source: Internet
Author: User

Synchronization block (method) of Java multi-thread synchronization mechanism-synchronized and multi-thread synchronized

During multi-threaded access, only one thread can use synchronized to modify the method or code block at the same time, solving the problem of resource sharing. The following code shows how to purchase five train tickets in three windows:

1 package com. jikexueyuan. thread; 2/* 3 * synchronized is not used, concurrency 4 */5 class RunnableDemo implements Runnable {6 private int tickets = 5; 7 @ Override 8 public void run () {9 for (int I = 0; I <10; I ++) {10 try {11 Thread. sleep (500); 12} catch (InterruptedException e) {13 e. printStackTrace (); 14} 15 if (tickets> 0) {16 System. out. println ("Ticket:" + tickets --); 17} 18} 19 20} 21} 22 23 public class ThreadTest {24 25 public static void main (String [] args) {26 RunnableDemo r = new RunnableDemo (); 27 Thread t1 = new Thread (r); 28 Thread t2 = new Thread (r); 29 Thread t3 = new Thread (r ); 30 t1.start (); 31 t2.start (); 32 t3.start (); 33} 34 35}

The result of one operation:

Ticket: 5 Ticket: 4 Ticket: 3 Ticket: 2 Ticket: 1 ticket: 2

After synchronized is used to synchronize a block:

1 package com. jikexueyuan. thread; 2/* 3 * use synchronized Blocks 4 */5 class RunnableDemo implements Runnable {6 private int tickets = 5; 7 @ Override 8 public void run () {9 for (int I = 0; I <10; I ++) {10 try {11 Thread. sleep (500); 12} catch (InterruptedException e) {13 e. printStackTrace (); 14} 15 synchronized (this) {16 if (tickets> 0) {17 System. out. println ("Ticket:" + tickets --); 18} 19} 20} 21} 22} 23 24 public class ThreadTest {25 26 public static void main (String [] args) {27 RunnableDemo r = new RunnableDemo (); 28 Thread t1 = new Thread (r); 29 Thread t2 = new Thread (r); 30 Thread t3 = new Thread (r ); 31 t1.start (); 32 t2.start (); 33 t3.start (); 34} 35 36}

Use the synchronized synchronization method:

1 package com. jikexueyuan. thread; 2/* 3 * use synchronized synchronization method 4 */5 class RunnableDemo implements Runnable {6 private int tickets = 5; 7 @ Override 8 public void run () {9 for (int I = 0; I <10; I ++) {10 show (); 11} 12} 13 public synchronized void show () {14 if (tickets> 0) {15 System. out. println ("Ticket:" + tickets --); 16} 17} 18} 19 20 public class ThreadTest {21 22 public static void main (String [] args) {23 RunnableDemo r = new RunnableDemo (); 24 Thread t1 = new Thread (r); 25 Thread t2 = new Thread (r); 26 Thread t3 = new Thread (r ); 27 t1.start (); 28 t2.start (); 29 t3.start (); 30} 31 32}

Whether synchronized synchronization block or synchronization method is used, the running results are reasonable:

Ticket: 5 Ticket: 4 Ticket: 3 Ticket: 2 Ticket: 1


Thinking: volatile is another synchronization mechanism. Can it? Reference my linked article: Java Theory and Practice: Correct Use of Volatile variable http://www.ibm.com/developerworks/cn/java/j-jtp06197.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.