3.8 Java Basics Summary ① multithreading

Source: Internet
Author: User

Multithreading

Multithreading This chapter theory is greater than the practice, because in the actual development of multi-threading are encapsulated inside the framework, programmers generally do not write multi-threading, multithreading belongs to the Java inside the lower code.

Threads are the most basic unit of processor scheduling
Programs > Processes > Threads
Program is dead, when the start of the program will have one or several processes, each process inside can have the Connaught Main line. Based on thread overhead more

Create Threads : two methods
1. Declare the subclass of Thread , overriding the Run method
Class MyThread extends thread{
public void Run () {
}
}
Mythread a = new Mythread ();
A.start;

2. Implement the Runnable interface and implement the Run method
Class MyThread2 implements runnable{
public void Run () {}
}
Mythread2 B = new MyThread2 ();
thread bb = new Thread (b);
Bb.start;
The calling thread implements the Runnable interface with a parameterized construct, which instantiates the object
This method is commonly used because Java is single-inheritance, but can be implemented more

The main methods of threading:
-start start, call the Run method, call the Run method directly equivalent to the normal method, not the thread
-sleep pause, static, how many milliseconds the thread can pause
-yield give up control of the CPU, let others do it first,

Set Priority (1-10) 10 Max, priority is not absolute first, or run out before running other
But more resources to seize

Wait-notify mechanism
Communication between threads
Can only be used in the synchronized inside,
Wait: Let the thread that uses the object stop running and wait until another thread that uses the object runs notify
Notify: Thread encounters notify in run, ring the first thread that encounters wait with the same object


Synchronization block, synchronous method
Synchronized
Locked, protected object, can be itself or other object,
When running to a synchronization block or synchronous method, other objects cannot run this side code, it is necessary to run this thread after the other to run the code of the side here
Ensures that some data is accurate (for example, many objects read and write (access) one resource at a time)

Synchronization blocks: You can lock your own objects or other objects, block code less than methods, reduce the wait time for other threads
Synchronization method: Can only lock the object itself,

3.8 Java Basics Summary ① multithreading

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.