Java thread synchronization-Ticket Selling

Source: Internet
Author: User


Thread Synchronization Method: (1) Synchronous Code block, format:

Synchronized (synchronization object ){

// Synchronize code

}

(2) synchronization method. format:

Add synchronized BEFORE THE METHOD


Problem:

Multiple people buy tickets at the same time.


1. Resources are not synchronized.

package thread;public class Tickets implements Runnable {private int count = 5;@Overridepublic void run() {for (int i = 0; i < 10; ++i) {if (count > 0) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(count--);}}}public static void main(String[] args) {Tickets tickets=new Tickets();        Thread t1=new Thread(tickets);        Thread t2=new Thread(tickets);        Thread t3=new Thread(tickets);        t1.start();        t2.start();        t3.start();    }}

Running result:

534221

Obviously, the result is wrong.

2. synchronous code block

Synchronization in the run method, that is, synchronization of shared resources (number of votes, count)

package thread;public class Tickets implements Runnable {private int count = 5;@Overridepublic void run() {for (int i = 0; i < 10; ++i) {synchronized (this) {if (count > 0) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(count--);}}}}public static void main(String[] args) {Tickets tickets = new Tickets();Thread t1 = new Thread(tickets);Thread t2 = new Thread(tickets);Thread t3 = new Thread(tickets);t1.start();t2.start();t3.start();}}

3. Synchronization Method

package thread;public class Tickets implements Runnable {private int count = 5;@Overridepublic void run() {for (int i = 0; i < 10; ++i) {sale();}}public synchronized void sale() {if (count > 0) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(count--);}}public static void main(String[] args) {Tickets tickets = new Tickets();Thread t1 = new Thread(tickets);Thread t2 = new Thread(tickets);Thread t3 = new Thread(tickets);t1.start();t2.start();t3.start();}}


Reference: http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html. We hereby declare that.





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.