Derived Thread class

Source: Internet
Author: User

One of the easiest ways to write thread-based code is to derive the Java.lang.Thread class. The thread class is a member of the Java.lang package and, by default, the thread class can be invoked by all Java applications. To use thread classes, we need to understand the five methods defined in the Java.lang.Thread class:

Run (): This method is used for thread execution. You need to overload the method so that the thread does a specific job.

Start (): This method causes the thread to start run ().

Stop (): This method, contrary to the action of the Start method, stops the running of the thread.

Suspend (): This method differs from the Stop method in that it does not terminate the unfinished thread, it simply suspends the thread and is recoverable later.

Resume (): This method restarts a thread that has been suspended.

Run the program in List A, run the results see list B

List A: Extending the Thread class

Class Testthreads {
public static void Main (String args []) {
Class Mythread extends Thread {
String which;
Mythread (String which) {
This.which = which;
}
public void Run () {
int iterations = (int) (Math.random () *100)% 15;
int sleepinterval = (int) (Math.random () *1000);
SYSTEM.OUT.PRINTLN (which + "running for" + iterations + "iterations");
SYSTEM.OUT.PRINTLN (which + "sleeping for" + Sleepinterval + "Ms between Loops");
for (int i = 0; < iterations; i++) {
SYSTEM.OUT.PRINTLN (which + "" + i);
try {
Thread.Sleep (Sleepinterval);
catch (Interruptedexception e) {}
}
}
}
Mythread a = new Mythread ("Thread a");
Mythread B = new Mythread ("Thread B");
Mythread C = new Mythread ("Thread C");
A.start ();
B.start ();
C.start ();
}
}
LISTB: Output from listing a
Thread A running for iterations
Thread C running for iterations
Thread B running for iterations
Thread A sleeping for 305ms between
Loops
Thread C sleeping for 836ms between
Loops
Thread B sleeping for 195ms between
Loops
Thread A 0
Thread C 0
Thread B 0
. . .
Thread C 13
Thread B 13
Thread A 14
Thread C 14
Thread A 15

List a demonstrates how to derive a new class from the existing thread class. The newly created class overloads the Run method. Interestingly, implementing the Run method does not have to be rigorous, because the thread class provides a default run method, although it is not particularly useful.

On some occasions, we cannot simply change the parent class of the specified object. We still need to adopt threads. At this point, we need to use the Runnable interface.

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.