Java language learning chapter summarizes the extended multi-threading concepts and use

Multithreading

Multithreading is the multiple simultaneous tasks, such as listening to music while playing the game at the same time.

Concurrent and Parallel

Concurrency: two or more events occur at the same time period. I.e. two or more events alternately in the CPU.
Parallel: two or more events all occurred at the same time, i.e. simultaneously performing two or more events in the CPU.

Threads and processes

process : refers to an application running in memory, each process has a separate memory space, an application can run multiple processes simultaneously; a process's execution is a program, running the program is the basic unit of the system ; the system is running a program that is a process from creating, running to the demise of the process.
Thread : A thread is a unit execution process, the current process is responsible for program execution, a process in which at least one thread. A process can have multiple threads, the application can also be called multi-threaded programs .
In short: there is at least one process after running a program, a process can contain multiple threads

The program runs, added to the process that is running in RAM memory.
When you click on a program in a specific function that event to the CPU will execute. Execution path is called a thread.
Analysis is as follows:
Thread analysis chart
(B picture to this network station class programmer horse)

Thread scheduling

● time-sharing scheduling
all the threads take turns using the right to use the CPU, the average time each thread CPU-allocation.
● preemptive scheduling
priority so that high-priority threads CPU, if the priority of the thread of the same, then will randomly select one (thread randomness), for the use of Java preemptive scheduling.

The main thread

The main thread: the primary thread of execution mian () method. When the JVM executes the main method, main method will enter into the stack memory. JVM will find the operating system to open up a main method of execution path leading to the cpu, cpu main method can be performed through this path, and this path has a name, called main (main) thread.

Create multi-threaded programs

Java uses java.lang.Thread class represents a thread, as some thread object must be an instance of the Thread class or subclass . The role of each thread is to complete a certain task is actually executing code stream of a program that is a period of the order of execution. Java uses thread of execution to represent this program flow.
Java step in and start to create multi-threaded through inheritance Thread class is as follows:

  1. Subclass definition of the Thread class, the class and override run () method, the method body of the run () method represents the thread needs to complete the task, so the run () method is called thread of execution .
  2. Create a Thread subclass instance, the thread object is created
  3. Call the thread object's start () method to start the thread

Create a new thread of execution in two ways. One way is to declare the class as a subclass of Thread. The method of the Thread class run subclasses should override.

Code examples:
First, create a Thread subclass:

//创建一个Thrad类的子类
public class MyThread extends Thread{
	//在Thread类的子类中重写run()方法,设置线程任务
	@Override
	public void run() {
		for(int i=0;i<20;i++) {
			System.out.println("run:" + i);
		}
	}
}

Call the Thread subclass modal class create a sub-class object:

public class ThreadClass {

	/*
	 * 创建多线程程序的第一种方式:创建Thread类的子类
		java.lang.Thread类:是描述线程的类,我们想要实现多线程程序,就必须继承Thread类
		实现步骤:
			1.创建一个Thread类的子类
			2.在Thread类的子类中重写Thread类中的run方法,设置线程任务(开启线程要做什么?)
			3.创建Thread类的子类对象
			4.调用Thread类中的方法start方法,开启新的线程,执行run方法
				void start() 使该线程开始执行; Java虚拟机调用该线程的run方法。
				结果是两个线程并发地运行;当前线程(main线程)和另一个线程(创建的新线程,执行其run方法)。
				多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。
		java程序属于抢占式调度,那个线程的优先级高,那个线程优先执行;同一个优先级,随机选择一个执行
	 */
	public static void main(String[] args) {
		//创建Thread类的子类对象
		MyThread mt = new MyThread();
		//调用Thread类中的start()方法,开启新县城,执行run方法
		mt.start();
		
		for(int i=0;i<20;i++) {
			System.out.println("main:" + i);
		}
	}

}

Output:
Output
visible simultaneously two methods for loop output. Each output may look different, random, Gonzalez, cpu
usage rights.

Multithreading principle

Figure analysis:
analysis
multi-threaded tasks when executed, would open up the stack space to perform different events.
Analysis Figure:
diagram

Get Thread name

Create a Thread subclass:

public class GetThreadNameClass extends Thread{
	@Override
	public void run() {
		//获取线程名称
		//方法一
		String name = getName();
		System.out.println(name);
		
		//方法二
		//Thread t = Thread.currentThread();
		//System.out.println(t);
	}

}

Create a test class

public class GetThreadNameTest {

	public static void main(String[] args) {
		//创建thread类的子类对象
		GetThreadNameClass mt = new GetThreadNameClass();
		mt.start();
		
		new GetThreadNameClass().start();
		new GetThreadNameClass().start();
		new GetThreadNameClass().start();
		
		//获取主线程的名字
		System.out.println(Thread.currentThread().getName());	
	}
}

Output:
Output

Set the thread name

Set the name of the thread:

  1. SetName method using the Thread class (name)
    void setName (String name) change the name of this thread equal to the argument name.
    2. Create a parameterized constructor, the name of the parameter passed thread; parameterized constructor calls the parent class, pass the thread name to the parent, so the parent class (Thread) to a name from the child thread
    Thread (String name) Allocates a new Thread object.

------------------------------------
dark horse programmer course (ask some pictures taken from this station b )

Published 72 original articles · won praise 3 · Views 6183

Guess you like

Origin blog.csdn.net/Ace_bb/article/details/104168392