Initial use of the thread class to create a process

Source: Internet
Author: User
Tags thread class

How do I create a thread?

Create thread mode One: Inherit the Thread class.

Step: 1, define a class that inherits the thread class.

2, overwrite the Run method in the thread class.

3, create a thread for the subclass object that directly creates the thread.

4, call the Start method to open the thread and invoke the thread's task to execute the Run method.

You can get the name of the thread through the thread's GetName thread-number (starting from 0)

The main thread's name is main.

Class Demo extends Thread{private string Name;demo (string name) {super (name);//this.name = name;} public void Run () {for (int x=0; x<10; x + +) {//for (int y=-9999999; y<999999999; y++) {}system.out.println (name+ ...). x= "+x+" ... name= "+thread.currentthread (). GetName ());}} Class ThreadDemo2 {public static void main (string[] args) {/* The purpose of creating a thread is to open an execution path to run the specified code and other code to run concurrently. The specified code that runs is the task of this execution path. The tasks of the main thread created by the JVM are defined in the main function. and the custom thread where is its task? The thread class is used to describe threads, which require tasks. So the thread class also describes the task. This task is represented by the Run method in the thread class. That is, the Run method is the function that encapsulates the custom thread that runs the task. Defined in the Run method is the task code that the thread will run. The thread is turned on in order to run the specified code, so only the thread class is inherited and the Run method is replicated. Define the running code in the Run method. */////thread t1 = new Thread ();D Emo d1 = new Demo ("Mong Choi");D emo d2 = new Demo ("Xiaoqiang");d 1.start ();//Open thread, call the Run method. D2.start (); System.out.println ("Over ...." +thread.currentthread (). GetName ());}}

What is the difference between calling run and calling start?

1 classDemoextendsThread2 {3     PrivateString name;4 Demo (String name)5     {6 //super (name);7          This. Name =name;8     }9      Public voidRun ()Ten     { One         int[] arr =New int[3]; ASystem.out.println (arr[3]); -          for(intx=0; x<10; X + +) -         { theSystem.out.println (".... x=" +x+ "... name=" +Thread.CurrentThread (). GetName ()); -         } -     } - } +  -  +  A  at classThreadDemo3 - { -      Public Static voidMain (string[] args) -     { -Demo D1 =NewDemo ("Wang Choi"); -Demo D2 =NewDemo ("Xiaoqiang"); in D1.start (); -          to D2.start (); +  -System.out.println (4/0);//throw new ArithmeticException (); the  *          for(intx=0; x<20; X + +) $         {Panax NotoginsengSystem.out.println (x+ "...." +Thread.CurrentThread (). GetName ()); -         } the     } +}

Initial use of the thread class to create a process

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.