Simple analysis of the creation and start-up of Java thread _java

Source: Internet
Author: User
Tags thread class

Objective

A thread is a sequential flow of control within a program. The CPU actually executes one at a point in time. It's just that we split the cup into multiple time slices, and because it's very fast, we look like multiple threads. Just like your time is divided into several pieces, so the overall seems to do things regularly, efficiency will be high, not to mention is the cup.

To create and start a thread:

(1) Defines a subclass of a Thread class and overrides the run() method of the class, which run() represents the task the thread needs to complete. So the run() method is called the thread execution body

(2) Creating Thread An instance of a subclass, that is, creating a thread object

(3) Invoke the thread object's start() method to start the thread

Source:

Create thread class public

class Firstthread extends thread

{

private int i

by inheriting the thread class; Overriding the Run () method, the method body of the run () method is the thread execution body public

void run ()

{for

(; i<100;i++)

{//When the thread class inherits the Threads class. Use this directly to get

the GetName () of the current data//thread object to return the name of the current thread

//So you can call the GetName () method directly to return the name of the current thread

System.out.println (GetName () + "" +i);

}

public static void Main (string[] args)

{

for (int i=0;i<100;i++)

{

//Call thread's CurrentThread () Method gets the current thread

System.out.println (Thread.CurrentThread (). GetName () + "" +i);

if (i==20)

{

//Create and start the first thread

new Firstthread (). Start ();

Create and start the second thread

new Firstthread (). Start ();

Run the interface:

Summarize

The above is the entire content of this article, I hope that the study and work to help. If you have questions, you can leave a message for discussion.

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.