Import Java.lang.Thread;
Import java.lang.Runnable;
Import Java.util.Date;
public class defineandstartthread{
Class Threada extends thread{
Private Date rundate;
Subclasses inheriting thread must implement the Run method, and the Run method in thread does nothing
public void Run () {
SYSTEM.OUT.PRINTLN ("Threada begin");
This.rundate = new Date ();
System.out.println ("Threada run:" +this.rundate);
System.out.println ("Threada end");
}
}
Class THREADB implements runnable{
Private Date rundate;
public void Run () {
SYSTEM.OUT.PRINTLN ("THREADB begin");
This.rundate = new Date ();
System.out.println ("threadb run:" +this.rundate);
System.out.println ("threadb end");
}
}
public void Startthreada () {
This is an object-oriented polymorphic feature that can be assigned directly to a parent class variable, but the runtime still behaves as a subclass attribute
Declaring a variable type as a parent class, the actual variable type being a subclass object
Thread Threada = new Threada ();
When the thread is enabled, the JVM calls the thread's Run method, executes the thread
Threada.start ();
}
public void startthreadb () {
Runnable RB = new threadb ();
Thread threadb = new Thread (RB);
When a thread is enabled, it causes the Run method that calls the thread to execute the thread
Threadb.start ();
}
public static void Main (string[] args) {
Defineandstartthread test = new Defineandstartthread ();
Test.startthreada ();
TEST.STARTTHREADB ();
}
}
ImportJava.lang.Thread;Importjava.util.Date; Public classstopthread{//because the start, stop, and main methods of the class use the Threada instance object, it is declared as a class variable PrivateThreada thread =NewThreada (); classThreadaextendsthread{//used to externally set the identity of whether the Run method is executed Private Booleanrunning =false; PrivateDate rundate; //override the Start method of the thread parent class, set run ID to true above the parent class Start method Public voidstart () { This. running =true; Super. Start (); } //Once the thread is started, the loop executes continuously until the execution is identified as false and stops running Public voidrun () {System.out.println ("Threada Begin"); Try{ intI=0; while( This. Running) { This. rundate =NewDate ();//System.out.println ("Threada run:" +this.rundate);System.out.println ("Threada run:" + i++); Thread.Sleep (200); } }Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Threada End"); } Public voidSetrunning (Booleanrunning) { This. running =running; } } Public voidstart () { This. Thread.Start (); } //Modify execution ID to false to stop execution of the Run method Public voidStop () { This. thread.setrunning (false); } Public Static voidMain (string[] args) {stopthread stopthread=NewStopthread (); //thread runs until end of resultStopthread.start (); //Current thread sleeps 1 seconds Try{Thread.Sleep (1000); }Catch(interruptedexception e) {e.printstacktrace (); } stopthread.stop (); } }
Start and stop of threads