JAVA processes and threads (a)

First, the concept of the city and threads
(a) process
each application operating system, these applications are a number of different tasks, which work together constitute the basic functions of the operating system.
The essence of the process is that some of the system memory, each process occupies a certain amount of resources.

(B) the threads
in the process contains one or more execution units (sub-tasks, threads), such as 360 security guards are: killing the virus can speed up, clean up the computer ...... and so on. These features are some of the subtasks of the current 360 security guards, the last of these tasks put together 360 security guards called the ultimate functional (process).
Thread also called lightweight processes.
A process contains one or more threads (but not not one).
Here Insert Picture Description
Particular thread:
(1) a thread can only belong to a process
(2) after an application up and running, we will first create a primary thread

(C) multi-threaded

		运行(){
		杀毒();   10min
		检查();   4min
		加速();   1min
		}
		对于之前的代码而言,如上的三个方法需要保证从上到下依次执行,并且前面一个方法没有执行完毕之前不能执行后面的方法。
		问题:由于每个任务的执行需要在之前任务结束后开始,所以会造成很多不必要的等待,浪费时间,浪费效率。

Solution: use multiple threads.

	在一个应用程序中,同时创建多个线程,每一个线程各自执行其自己的逻辑(彼此之间的逻辑应该是互相独立的,互不干扰的),这样在一个执行的同时另外的线程也在执行,即所谓的并发执行。并发执行能够程序的执行效率,节约时间。

并发的概念:不是真正意义上的同时执行,CPU将时间进行分割得到很多的时间片,即线程执行所必须的资源,而所有的线程如果要执行则需要得到时间片,在多线程同时存在的情况下,线程会主动去竞争时间片,只有得到时间片以后线程才能执行。所以如果线程数很多而资源很少的时候,必定有线程是处于等待状态的。由于资源的抢夺速度很快,所以从宏观角度看并不觉得有先后顺序,而在微观角度,线程其实是走走停停的。

The relationship between Java programs with multiple threads

(1)首要线程:执行main方法的时候,main方法作为入口函数,从此处使这个程序继续往下执行
(2)垃圾回收线程:在程序正常执行的同时,由于会产生很多的对象,而堆内存中有大小上限,垃圾回收器会主动检查无用的或长期不使用的对象,将其占据的内存空间释放。
(3)可能还有其它的线程……

Second, create a thread

	1、继承Thread类
	定义一个类,用来表示线程,该类需要实现Thread类
	需要重写继承自Thread类中的run方法
	public class Demo01 {
	    public static void main(String[] args) {
	        //创建线程对象
	        Thread t1 = new MyThread01();
	        //启动线程
	        //启动线程应该使用start方法,run方法虽然可以执行具体逻辑,但是不会创建新的线程对象
	        //而start方法在执行run方法的同时也会一个开辟一个新的线程,而这正是多线程所需要的
	        t1.start();
	    }
	}

	/**
	 * 定义一个类,用来表示线程,该类需要实现Thread类
	 */
	class MyThread01 extends Thread {
	    @Override
	    public void run() {
	        System.out.println("这是一个线程");
	    }
	}

2, implement Runnable

			/**
			 * 线程对象的创建--实现Runnable接口
			 */
			public class Demo02 {
			    public static void main(String[] args) {
			        Thread t1 = new Thread(new MyThread02());
			        //也通过start去启动线程
			        t1.start();
			
			        //也可以通过匿名内部类实现Runnable接口
			        Thread t2=new Thread(new Runnable(){
			            @Override
			            public void run() {
			                System.out.println("通过匿名内部类创建的线程...");
			            }
			        });
			        t2.start();
			    }
			}
			
			class MyThread02 implements Runnable {
			    @Override
			    public void run() {
			        System.out.println("这是第二个线程...");
			    }
			} 

Multithreading features: concurrent execution of multiple threads, the order is uncertain.

Third, the thread associated API

	线程的两种调度方式
	(1)分时调度模型。所有线程轮流使用CPU的使用权,平均分配每个线程占用CPU的时间片。
	(2)抢占式调度模型。优先让优先级高的线程使用CPU,若相同,则随机选择,优先级高的线程获取CPU的时间片相对多一些。
	Java使用的是抢占式调度模型。
	
	优先级
	线程的优先级划分为1-10级,其中1最低,10最高,线程提供了3个常量来表示最低、最高以及默认优先级。线程的调度是由线程调度控制的,可以通过提高线程的优先级来最大程度改善线程获取时间片的几率。但不一定就能保证优先级高的就先获得时间片。
	
	join方法和yield方法的区别
	void join()
	该方法用于等待当前线程的结束
	void yield()
	该方法用于使当前线程主动让出当次CPU时间片回到就绪状态,等待时间片分配。使得多个线程的执行更和谐,但是保证不了严格交替执行。

Guess you like

Origin blog.csdn.net/weixin_43727372/article/details/90299396