Start and stop of threads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.