[Html] <span style = "color: # 3366ff;">/* implement the Runnable interface to create a Thread class to share a data </span> [html] <span style = "color: # 3366ff; "> compile a train station ticketing program -- sell tickets in three windows at the same time </span> <span style =" color: # 3366ff; "> */class PP implements Runnable {// class PP public int tickets = 100 that implements a Runnable interface; // a total of 100 tickets String str = new String ("123"); // create a String object to pass the local name to the public void run () {while (true) parameter) {// when the condition is true synchronized (this. str) {// (the synchronization parameter must be the name of an object) // when the tt thread determines that the condition is true, lock str and execute internal code, when the internal code is not fully executed, the CPU may be snatched by other threads, but when it is determined that there is a lock in synchronization, it will be waiting outside and cannot enter, ensure that only one thread is executed internally. After tt executes the internal code and exits, it will seize the CPU execution right on the same starting line as tt1. if (tickets> 0) {System. out. println (Thread. currentThread (). getName () + "sold tickets:" + tickets); tickets --;} else {break ;}}}}} public class Threade_14 {public static void main (String [] args) {PP pp = new PP (); Thread tt1 = new Thread (pp ); // construct the Thread object to implement the Runnable class PP Thread tt2 = new Thread (pp); Thread tt3 = new Thread (pp); tt1.start (); // enable the Thread tt2.start (); tt3.start () ;}</span>