Java Learning-Multi-threading

Source: Internet
Author: User

This article is based on a 5-year hammer practice-the most basic Java video for beginners in history

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

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

Multithreading Benefits: Solves the problem of multiple parts running at the same time.

The disadvantage of multithreading: too many threads back to the reduction of efficiency.


When the JVM starts, it starts multiple threads, and at least two threads can parse it out.

1, the thread that executes the main function,
The thread's task code is defined in the main function.

2, the thread responsible for garbage collection.


How do I create a thread?

Create thread mode One: Inherit the Thread class.

Steps:
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.

 class  Demo extends   thread{ private   String name;    Demo (String name) { super   (name);  // this.name = name;      run () { for  (int  x=0; x<10; X++  System.out.println (name+ ".... x=" +x+ "... name=" +thread.currentthread (). GetName ()); }}} 

class ThreadDemo2 {
Public static void main (string[] args) {
Demo D1 = new Demo ("Wang Choi");    Demo D2 = new Demo ("Xiaoqiang"); D1.start ();//Open thread, call the Run method.    D2.start ();  System.out.println ("over ...." +Thread.CurrentThread (). GetName ()); }}

Java Learning-Multi-threading

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.