Thread-based conceptual stuff

Source: Internet
Author: User

1. What is a thread
* Thread is a path for program execution, one process can contain multiple threads
* Multi-threaded concurrent execution can improve the efficiency of the program, can complete a number of tasks at the same time
* 2. Multi-Threaded application scenarios
* Starscream sharing screen to multiple computers simultaneously
* Thunder open multiple threads to download together
* QQ at the same time and many people together video
* The server processes multiple client requests at the same time

# # #24.02_ Multithreading (differences between multithreading parallelism and concurrency) (learn)
* Parallel is two tasks at the same time, is a mission to carry out while the B task is also in progress. (Multi-core CPU required)
* Concurrency means that two tasks are requested to run, and the processor can only be subject to a task, the two tasks are scheduled to take turns, because of the short time interval, makes the sense that both tasks are running.
* For example, I chat with two netizens, left hand operation a computer with a chat, while the right hand with another computer with B chat, this is called parallel.
* If you use a computer I send a message first, and then immediately send a message to B, and then chat with a, and then chat with B. This is called concurrency .

There are two ways to implement threads:

1. Inherit thread
* Define class Inheritance Thread
* Override the Run method
* Write what the new thread is going to do in the Run method
* Create Thread Object
* Start a new thread and the Run method is automatically executed internally
*

public class Demo2_thread {

/**
* @param args
*/
public static void Main (string[] args) {
MyThread MT = new MyThread ();//4, creating an object of the custom class
Mt.start ();//5, opening thread

for (int i = 0; i <; i++) {
System.out.println ("BB");
}
}

}
Class MyThread extends Thread {//1, defining classes inheriting thread
public void Run () {//2, overriding the Run method
for (int i = 0; i < i++) {//3, code to be executed, written in the Run method
System.out.println ("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
}
}

2. Implement Runnable
* Define class implementation Runnable interface
* Implement the Run method
* Write what the new thread is going to do in the Run method
* Create a custom Runnable subclass object
* Create thread object, incoming runnable
* Call Start () to open a new thread, the internal will automatically call Runnable's Run () method

public class Demo3_runnable {
/**
* @param args
*/
public static void Main (string[] args) {
Myrunnable Mr = New Myrunnable ();//4, creating a custom class object
Runnable target = new myrunnable ();
Thread t = new thread (MR);//5, which is passed as an argument to the constructor of thread
T.start ();//6, opening thread

for (int i = 0; i <; i++) {
System.out.println ("BB");
}
}
}

Class Myrunnable implements Runnable {//1, custom class implementation Runnable interface
@Override
public void Run () {//2, overriding the Run method
for (int i = 0; i < i++) {//3, code to be executed, written in the Run method
System.out.println ("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
}

}

The way to implement two threads with anonymous inner classes

Inherit the thread class

The new Thread () {//1,new class () {} Inherits this class
public void Run () {//2, overriding the Run method
for (int i = 0; i < i++) {//3, code to be executed, written in the Run method
System.out.println ("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
}
}.start ();
* Implement Runnable interface

The new Thread (new Runnable () {//1,new interface () {} Implements this interface
public void Run () {//2, overriding the Run method
for (int i = 0; i < i++) {//3, code to be executed, written in the Run method
System.out.println ("BB");
}
}
}). Start ();

Multithreading (get first name and set name)

1. Get the name
* Get the name of the thread object through the GetName () method
* 2. Set the name
* A String type name can be passed through the constructor function
*
New Thread ("xxx") {
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (This.getname () + ".... aaaaaaaaaaaaaaaaaaaaaaa");
}
}
}.start ();

New Thread ("yyy") {
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (This.getname () + ".... bb");
}
}
}.start ();
* The name of the thread object can be set by the SetName (String) method
*
thread T1 = new Thread () {
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (This.getname () + ".... aaaaaaaaaaaaaaaaaaaaaaa");
}
}
};

Thread t2 = new Thread () {
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (This.getname () + ".... bb");
}
}
};
T1.setname ("Sister Furong");
T2.setname ("Sister Feng");

T1.start ();
T2.start ();

Gets the current thread with thread.current.

Thread.CurrentThread (), the main thread can also get
*
New Thread (New Runnable () {
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (Thread.CurrentThread (). GetName () + "... aaaaaaaaaaaaaaaaaaaaa");
}
}
}). Start ();

New Thread (New Runnable () {
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (Thread.CurrentThread (). GetName () + "... bb");
}
}
}). Start ();
Thread.CurrentThread (). SetName ("I am the main thread");//Get a reference to the main function thread and change the name
System.out.println (Thread.CurrentThread (). GetName ());

Use of the thread join method

The current thread pauses, waits for the specified thread to finish executing, and then continues

Final thread T1 = new Thread () {
public void Run () {
for (int i = 0; i < i++) {
System.out.println (This.getname () + "... aaa");
}
}
};
Thread t2 = new Thread () {
public void Run () {
for (int i = 0; i < i++) {
if (i = = 2) {
try {
T1.join ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
System.out.println (This.getname () + ".... BBB");
}
}

};
T1.start ();
T2.start ();
}
}

Multi-threaded (sets the priority of the thread)
* SetPriority () Sets the priority of the thread

Thread-based conceptual stuff

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.