Java Thread Programming 1.2-a simple two-thread example

Source: Internet
Author: User

There are two ways to create a new class that can have a thread running within it. one way is to extend the Thread class. the other is to extend any class and implement the Runnable interface. for the sake of authentication, extending Thread is the simplest approach and is initially used in this book. implementing the Runnable interface tends to work much better in the real world; this technique is introduced in Chapter 4, "Implementing Runnable Versus Extending Thread."

There are two ways to create thread classes:

  • Implement the Runnable interface, which can inherit from any class
  • Inherit Thread class

Obviously, the first method is flexible.

The steps to spawn a new thread in this chapter's example are

• Extend the java. lang. Thread class.

• Override the run () method in this subclass of Thread.

• Create an instance of this new class.

• Invoke the start () method on the instance

To create a thread:

  • Inherit from java. lang. Thread class
  • Override the run () method of the subclass
  • Create a new class instance
  • Call the start () method of the instance.

A call to start () returns right away and does not wait for the other thread to begin execution. in start (), the parent thread asynchronously signals through the JavaVM that the other thread shoshould be started as soon as it's convenient for the thread schedent. at some unpredictable time in the very near future, the other thread will come alive and invoke the run () method of the Thread object (or in this case, the overridden run () method implemented in TwoThread ). meanwhile, the original thread is free to continue executing the statements that follow the start () call.

The sub-parent thread runs asynchronously. After start () is called, the sub-thread starts to execute for a certain time.

Important to note is that although the order in which each thread will execute its own statements is known and straightforward, the order in which the statements will actually be run on the processor is indeterminate, and no special order shoshould be counted on for program correctness.

Sequential execution in the thread. The execution sequence between threads is unknown.


Listing 2.1 TwoThread. java-The Complete Code for the TwoThread Example
1: public class TwoThread extends Thread {
2: public void run (){
3: for (int I = 0; I <10; I ++ ){
4: System. out. println ("New thread ");
5 :}
6 :}
7:
8: public static void main (String [] args ){
9: TwoThread tt = new TwoThread ();
10: tt. start ();
11:
12: for (int I = 0; I <10; I ++ ){
13: System. out. println ("Main thread ");
14 :}
15 :}
16 :}

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.