Package exception exercise;
Class Seel implements runnable{
private String name;
private int ticket=100;
Seel (String name) {
This.name=name;
}
public void Run () {
while (true) {
if (ticket>0) {
System.out.println (Thread.CurrentThread (). GetName () + "ticketing" + "..." + "Balance of" + (--ticket));
}
}
}
}
public class Text26 {
public static void Main (string[] args) {
Seel s=new Seel ("Ticket machine 1");
/*seel s2=new Seel ("Ticket Machine 2");
Seel s3=new Seel ("Ticket machine 3");
Seel s4=new Seel ("Ticket machine 4");
Seel s5=new Seel ("Ticket machine 5");
*/
Thread t1=new thread (S);
Thread t2=new thread (S);
Thread t3=new thread (S);
Thread t4=new thread (S);
Thread t5=new thread (S);
T1.start ();
T2.start ();
T3.start ();
T4.start ();
In the ticketing system we can not new out many sub-class, because they are relatively independent, multi-threaded opening is not interfering with each other,
And the total number of tickets in the ticket machine is shared, all of us to use the same sub-class, and then the subclass as
Parameters are passed to the tread class, and then let tread new out multiple objects to realize the phenomenon of multiple ticket machines.
This way, although we open the thread's multithreaded pattern, the Seel class is unique because it passes in the parameters, which causes the methods and properties in this class to share the common effect.
}
}
........................
Class Seel implements runnable{
private String name;
private int ticket=100;
Seel (String name) {
This.name=name;
}
public void Run () {
while (true) {
if (ticket>0) {
System.out.println (Thread.CurrentThread (). GetName () + "ticketing" + "..." + "Balance of" + (--ticket));
}
}
}
}
public class Text29 {
public static void Main (string[] args) {
Seel s=new Seel ("ticketing");
Thread T1=new thread (S, "Ticket machine 1");
Thread T2=new thread (S, "Ticket Machine 2");
Thread T3=new thread (S, "Ticket machine 3");
Thread T4=new thread (S, "Ticket machine 4");
Thread T5=new thread (S, "Ticket machine 5");
T1.start ();
T2.start ();
T3.start ();
T4.start ();
In this improvement I found that the thread constructor has a type of thread (Runnable target, string name) {}; The string name inside is passed the message,
Then this message will overwrite the one that's automatically named in the thread.
}
}
The thoughts of multi-thread ticketing system