One of the simplest ways to write thread-based code: to derive a Thread class

Source: Internet
Author: User
One of the simplest ways to write thread-based code: the derived Thread class-general Linux technology-Linux programming and kernel information. For more information, see the following. Derived Thread class

One of the simplest ways to write Thread-based code is to derive the java. lang. Thread class. This thread class is a member of the java. lang package. By default, the thread class can be called by all Java applications. To use The Thread class, 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 reload this method so that the thread can do specific work.

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

Stop (): This method is opposite to the start method. It stops the running of the thread.

Suspend (): Unlike the stop method, this method does not terminate unfinished threads. It only suspends threads and can be recovered later.

Resume (): This method restarts a suspended thread.


Run the program in List A. For the running result, see List B.



List A: Extended 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 ");

MyThread B = new MyThread ("Thread B ");

MyThread c = new MyThread ("Thread C ");

A. start ();

B. start ();

C. start ();

}

}



ListB: output of listing

Thread A running for 16 iterations



Thread C running for 15 iterations

Thread B running for 14 iterations

Thread A sleeping for 305 ms

Loops

Thread C sleeping for 836 ms

Loops

Thread B sleeping for 195 ms

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 generate A new class from the existing Thread class. The newly created class reloads the run method. Interestingly, you do not need to strictly implement the run method, because the Thread class provides a default run method, although it is not particularly useful.
In some cases, we cannot simply change the parent class of the specified object. We still need to use threads. In this case, we need to use the Runnable interface.
Related Article

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.