/* Process: is an executing program each process execution has an execution order, which is an execution path, or a control unit thread: a process in which a separate control unit thread is executing a process that controls the execution of a procedure at least one thread the Java VM starts with a Cheng Java.exe at least one thread in this process replicates the execution of a Java program that runs code that exists in the main method, which is called the main thread JVM to start more than one thread, as well as threads that replicate the garbage collection mechanism: (1) inherit the thread class; The replication Run method, create the object, call the Start Method (2) to implement the Runnable interface, the replication run method, establish the thread object, pass the Runnable sub-class object to the thread class constructor */package Pack;public class Main { public static void Sys (Object obj) {System.out.println (obj); } public static void Main (string[] args) {//method1 (); Method2 (); Inherit the thread method to create threads, instance: 4 window Sell ticket method3 ();//implement runnable method create thread, instance: 4 window Sell ticket} public static void Method1 () {Demo D1 = new Demo ("One---"); Create a thread demo D2 = new Demo ("Two---"); Create a thread d1.start (); D2.start (); for (int i=0;i<20;i++) SYS ("main run"); } public static void Method2 () {Ticket T1 = new Ticket (); Ticket t2 = new Ticket (); Ticket t3 = new Ticket (); Ticket T4 = new Ticket (); T1.start (); T2.start (); T3.start (); T4.start (); } public static void Method3 () {Ticketdemo t = new Ticketdemo (); thread T1 = new Thread (t); Thread t2 = new Thread (t); thread t3 = new Thread (t); thread T4 = new thread (t); T1.start (); T2.start (); T3.start (); T4.start (); }}class Demo extends Thread {demo (String name) {super (name);//Parent class defines GetName method, implements custom thread name, defaults to thread0,1} p ublic void Run () {for (int i=0;i<20;i++)//main.sys (this.getname () + "demo Run"); The next two sentences are no different, CurrentThread gets the current thread object, which is this main.sys (Thread.CurrentThread (). GetName () + "demo Run"); }}class Ticket extends Thread {static int tickets = 200;//200 tickets, be sure to static,4 window common 200 public void Run () {W Hile (True) {if (tickets>0) Main.sys (this.getname () + "---" +tickets--); }}}class Ticketdemo implements Runnable {int tickets = 100; Public VOID Run () {while (true) {if (tickets>0) Main.sys (Thread.CurrentThread (). GE Tname () + "---" +tickets--); } } }
Java creation thread two methods