Two methods of multi-thread ticketing: Synchronous Code block locking and multi-thread Ticketing

Source: Internet
Author: User

Two methods of multi-thread ticketing: Synchronous Code block locking and multi-thread Ticketing

Package com. swift. duoxiancheng; class Ticket extends Thread {Ticket (String name) {super (name); // constructor: set Thread name} public static int ticket = 1000; @ Override public void run () {while (true) {synchronized ("Lock") {if (ticket> 0) {ticket --; System. out. println (Thread. currentThread (). getName () + "remaining votes:" + ticket) ;}}} public synchronized void sellTickets () {}} public class ThreadTest_Jicheng {pu Blic static void main (String [] args) {// One is that multiple response processes correspond to one object (such as a ticket ), one is that multiple response processes correspond to multiple objects (such as socket response) Ticket t1 = new Ticket ("Ticket window 1 "); ticket t2 = new Ticket ("Ticket window 2"); Ticket t3 = new Ticket ("Ticket window 3 "); ticket t4 = new Ticket ("Ticket window 4"); // The preceding Ticket generates four pairs, each of which has 1000 million lines to be exported, the reason is that the CPU is allocated with a forward slash (/), and then the CPU is allocated with a forward slash (/). The vote is not empty for the target image, in the internal storage method area, the synchronization problem should be solved by t1.start (); t2.start (); t3.start (); t4.start (); System. out. println ("Hello World! ");}}

The above method inherits the Thread

This method is relatively simple. You only need to re-write the run method after inheritance. The disadvantage is that you cannot inherit other classes and have limitations. Multithreading for operations on members in a single object requires static keywords.

The following is the implements Runnable method.

Package com. swift. duoxiancheng; class Tickets implements Runnable {private int ticket = 1000; @ Override public void run () {resume ();} // after synchronized is added to the method with loops, only one thread sells tickets because of locking a thread execution loop. Of course, only one thread is selling tickets, the lock should be added to the loop. // in the loop, there are still threads waiting outside the loop, and the negative number of votes finally occurs, because there is also synchronization outside the while, and the method does not sell tickets cyclically, only one ticket is sold. // The result will be affected when the result is written in the while statement. The public void vote () {while (true) {synchronized (this) {if (ticket> 0) will appear) {try {Thread. sleep (0);} catch (Inte RruptedException e) {e. printStackTrace ();} ticket --; System. out. println (Thread. currentThread (). getName () + "Additional ticket:" + ticket) ;}}} public class SellTickets_Runnable {public static void main (String [] args) {Tickets t = new Tickets (); Thread t1 = new Thread (t, "ticket window 1"); Thread t2 = new Thread (t, "ticket window 2 "); thread t3 = new Thread (t, "ticket window 3"); Thread t4 = new Thread (t, "ticket window 4"); t1.start (); t2.start (); T3.start (); t4.start (); System. out. println ("Hello World! ");}}

The lock can be either in the method or in the synchronous code block. The Synchronous Code block is better and can be partially locked.

The lock can make the object synchronized (this) or the string synchronized ("Lock ")

When locking, note that if the lock is a loop, there is only one thread to execute until it is completed.

Package com. swift. duoxiancheng; class Ticketz implements Runnable {private int ticket = 100; @ Override public void run () {random () ;}// after synchronized is added, only one thread is allowed to sell tickets, lock a Thread execution loop, of course, only one Thread is selling tickets public synchronized void resume () {while (ticket> 0) {try {Thread. sleep (5);} catch (InterruptedException e) {e. printStackTrace ();} ticket --; System. out. println (Thread. currentThread (). getName () + "Additional ticket:" + ticket ); }}} Public class SellTickets_OnlyOneThreadSell {public static void main (String [] args) {Ticketz t = new Ticketz (); Thread t1 = new Thread (t, "ticket window 1"); Thread t2 = new Thread (t, "ticket window 2"); Thread t3 = new Thread (t, "ticket window 3 "); thread t4 = new Thread (t, "ticket window 4"); t1.start (); t2.start (); t3.start (); t4.start (); System. out. println ("Hello World! ");}}

 

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.