Multi-threaded Multiple window selling tickets & communication between threads

Source: Internet
Author: User
Tags ticket

Case one: Use multi-threaded to complete three windows sell tickets (can not be repeated sell tickets and negative votes)

Ticket sales procedure Sellticket Use the methods in the lock class to implement lock and release locks!

Package Cn.itcast.thread2;import Java.util.concurrent.locks.lock;import Java.util.concurrent.locks.ReentrantLock; public class Sellticket implements Runnable {private int tickets = 100;private Lock lock=new reentrantlock ();p rivate objec T obj=new Object (), @Overridepublic Void Run () {while (true) {Lock.lock ();//You can also use the code in synchronized (obj) {if} to use the same lock if ( Tickets > 0) {try {thread.sleep (100);//Break for a while} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "selling First" + tickets--+ "ticket"); Lock.unlock ();//Release Lock}}}
Test class

Package Cn.itcast.thread2;public class Selltickettest {public static void main (string[] args) {sellticket s1=new Selltick ET (); Thread t1=new thread (S1); Thread t2=new thread (S1); Thread t3=new Thread (S1), T1.setname ("window One"), T2.setname ("window Two"), T3.setname ("window three"); T1.start (); T2.start (); T3.start ();}}
Case two: communication between threads

Communication between threads is the operation of the same data: example--

such as selling steamed buns, if bun shop without steamed buns let manufacturers produce steamed buns, while customers waiting. If steamed buns on the bun to sell buns, manufacturers can not produce steamed buns, waiting for the bun shop to produce buns after selling! So the producer waits!


Code:

Student class Student

Package Cn.itcast.thread3;public class Student {int Age;boolean flag;//default is no data false if there is data is true String name;}
To set a thread:

Package cn.itcast.thread3;/** * Communication between threads * If you have a student, it's not set. * @author Administrator * */public class Setthread implements Runnab Le{public Student s;private int x=0;public setthread (Student s) {this.s=s;} @Overridepublic void Run () {while (true) {synchronized (s) {///student object to be manipulated as lock object if (S.flag) {//judgment there is no data  If there is data, wait for the lock try {s.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} Create data without data if (x%2==0) {//is an even number to create Li Weikang s.name= "Li Weikang"; s.age=23;} else{//is an odd number to create Anlu s.name= "Anlu"; s.age=22;} x++;//x++//If you have already set up the data, change the tag s.flag=true;//have the data and wait for it to be said that the change is marked as trues.notify ();//Wake}}}
Get Thread:

Package Cn.itcast.thread3;public class GetThread implements Runnable{public Student s;public GetThread (Student s) { This.s=s;} @Overridepublic void Run () {while (true) {synchronized (s) {if (!s.flag) {///If flag is false means that no data is obtained, wait for the try {s.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} Print System.out.println with data ("Student's name and age are:" +s.name+ ":" +s.age);//change tag s.flag=false;//No data, wait for//Wake up S.notify ();/}}}}
Test class:

Package Cn.itcast.thread3;public class Test {public static void main (string[] args) {//Create resource Student s=new Student ();//Set and receive The Class GetThread get=new GetThread (s); Setthread set=new Setthread (s); Thread t1=new thread (get); Thread T2=new thread (set);//Start thread T1.start (); T2.start ();}}
Operation Result:










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Multi-threaded Multiple window selling tickets & communication between threads

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.