Multi thread ticketing case
Tickets implements runnable:
Public class tickets implements runnable {/ / define the ticket source that can be sold private int ticket = 100; public void run() {while (true) {/ / judge the number of votes and if it is greater than 0, it can be sold if (ticket > 0) {system.out.println (thread. Currentthread(). Getname() + "sold No." + ticket --;}}}
}
Test class Demo:
/** multiple threads access the same data resource concurrently * 3 threads for one ticket resource, Sell * / public class threaddemo {public static void main (string [] args) {/ / create a runnable interface to implement class objects tickets t = new tickets(); / / create three thread class objects and pass the runnable interface implementation class thread t0 = new thread (T); thread T1 = new thread (t); thread T2 = new thread (T); t0. Start(); T1. Start(); T2. Start();}}