Summary of Java Multi-threading

Source: Internet
Author: User

1-----------------------------------Basic Concepts-------------------------------------------------

(1) Multithreading: One application has multiple execution paths
Process: The application being executed
Threads: Execution unit of a process, execution path
Single thread: One application has only one execution path
Multithreading: One application has multiple execution paths
What is the meaning of multi-process?
Improve CPU utilization
What is the meaning of multithreading?
Increase application usage

(2) Multi-threaded implementation scheme
A: Inherit the Thread class
B: Implement Runnable interface

(3) Scheduling of threads
A: Scheduling of threads
A: time-sharing scheduling
B: Preemptive scheduling (Java is using this scheduling method)

(4) Resolve thread safety issues synchronously
A: Synchronizing code blocks
Synchronized (object) {
Code that needs to be synchronized;
}

The lock object here can be any object.

B: Synchronization method
Add synchronization to the method.

The lock object here is this

C: Static Synchronization method
Add synchronization to the method.

The lock object here is the byte-code file object of the current class (reflection-re-bytecode file object)

(5) The life cycle of a thread

2-----------------------------------Base Code-------------------------------------------------

(1) Inherit thread

/**
 * inherit thread
 * @author Administrator
 *
 */
   public class XC1 Extends Thread {

           String BB =null;
    
           Public XC1 () {}
    
           public XC1 (String aa) {
              & nbsp;  super ();
                 bb=aa;
    }
    
     @Override
    public void Run () {
           for (int i = 0; i < i++) {
             &N Bsp     SYSTEM.OUT.PRINTLN ("Thread 1:" +1111+BB);
        }
    }
}
(2) Implement Runnable interface


public class XC2 implements Runnable {


public void Run () {
for (int i = 0; i <; i++) {
SYSTEM.OUT.PRINTLN ("Thread 2:" +2222);
}
}

}
(3) Call


public class Demo1 {

public static void Main (string[] args) {

XC1 xc1 = new XC1 ();
Thread Th1 = new Thread (XC1);
Th1.start ();
XC2 Xc2 = new XC2 ();
Thread Th2 = new Thread (XC2);
Th2.start ();

}

}

Summary of Java 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.