Java Thread (i)

Source: Internet
Author: User

Java Thread (i)

1. Process

Process: A program that is in progress.

Thread: A control unit (execution path) in the process that is responsible for program execution

Multiple paths can be executed in a thread, called multithreading.

A. There is at least one thread in a process.

B. Multiple threads are turned on to run multi-part code at the same time.

C. Each thread has its own running content. This content can be called a task that the thread will perform.

D. Multithreading benefits: Troubleshooting Multi-part operations

E. Disadvantages: Open more cards.

In fact, the implementation of the application is the CPU is doing a quick switch to complete, this switch is also random.

The JVM starts a thread and has at least two threads.

A. The thread that executes the main function.

The thread's task code is defined in the main function.

B. The thread responsible for garbage collection

2. Thread code

A. How do I create a thread?

Create a threading method one:

1. Inherit the thread class.

2. Overwrite the Run method in the thread class (defined in the Run method is the task code that the thread will run).

3. Create a thread for the subclass object that directly creates the thread class.

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

Code:

Class Dome extends Thread {

private String name;

Dome (String name) {

THIS.name = name;

}

public void Run () {

System.out.println ("name=" + name);

}

}

Class threaddome2{

public static void Main (string[] args) {

Dome D1 = new Dome ("Xiaohei");

Dome D2 = new Dome ("Xiaoming");

D1.start ();

D2.start ();

}

}

Java thread (i)

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.