Java basic review: thread Exercise 2

Source: Internet
Author: User

Modify Exercise 1 to sell tickets. First, let it delay the network and solve the problem.

 

1) Synchronous Code Block

Class Ticket implements Runnable {private Integer num = 50; @ Overridepublic void run () {for (int I = 0; I <200; I ++) {sale ();}} private Object o = new Object (); // as the listening Object/*** ticket seller */public void sale () {synchronized (o) {if (num> 0) {try {Thread. sleep (1);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "sold" + num -- + "tickets") ;}}} public class TicketDemo {public static void main (String [] args) {Runnable target = new Ticket (); new Thread (target, ""). start (); new Thread (target, "B "). start (); new Thread (target, "C "). start ();}}

 

2) Method 2: Synchronization Method

 

Class Ticket implements Runnable {private Integer num = 50; @ Overridepublic void run () {for (int I = 0; I <200; I ++) {sale ();}} /*** sell tickets */public synchronized void sale () {if (num> 0) {try {Thread. sleep (1);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "sold" + num -- + "") ;}} public class TicketDemo {public static void main (String [] args) {Runnable target = new Ticket (); new Thread (target, ""). start (); new Thread (target, "B "). start (); new Thread (target, "C "). start ();}}

3) reentrant lock

 

Import java. util. concurrent. locks. reentrantLock; class Ticket implements Runnable {private Integer num = 50; private ReentrantLock lock = new ReentrantLock (); @ Overridepublic void run () {for (int I = 0; I <200; I ++) {sale () ;}/ *** sell tickets */public void sale () {lock. lock (); if (num> 0) {try {Thread. sleep (1); System. out. println (Thread. currentThread (). getName () + "sold" + num -- + "tickets");} catch (Exception e) {e. printStackTrace ();} finally {lock. unlock () ;}}} public class TicketDemo {public static void main (String [] args) {Runnable target = new Ticket (); new Thread (target, ""). start (); new Thread (target, "B "). start (); new Thread (target, "C "). start ();}}

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.