JAVA multi-threaded base

 

The difference between threads and processes

     1. threads and processes

  Each program running on the system is a process. Each process contains one or more threads. A thread is a set of a set of instructions, or the special section of the program, which can be independently executed in the program. It can also be understood as context code run. So basically a lightweight process thread, which is responsible for performing multiple tasks in a single program. Usually responsible for scheduling and execution of multiple threads by the operating system. Using threads can occupy a long time in the program into the background task to deal with, running speed of the program is likely to accelerate in the realization of some tasks, such as waiting for user input, file read and write, and send and receive network data, the thread is more useful . In this case you can free up some valuable resources such as memory usage and so on. If a large number of threads , will affect the performance, because the operating system needs to switch between them, more threads require more memory space, thread suspension need to consider their impact on the program running. Typically model data block is shared among a plurality of threads, the thread needs to prevent a deadlock situation.

     

 

  Summary: The process is the set of all threads, each thread is a path of execution process

 

     2. Why use multithreading

   

   Think about a problem:

    Foolish Old Man in front of a mountain, the Foolish Old Man if you want to remove, then the mountain takes about 200 years, the Foolish Old Man can not live so long, so is there any way you can remove the Foolish Old Man of the mountain in their lifetime?

  Solution:

    Yu Gong called the move with their children and grandchildren can move the mountain in their lifetime, multithreading is to improve the efficiency of

    Summary: Multithreading can improve the efficiency of the program

  3. multithreaded application scenarios

    Mainly to improve efficiency in a multithreaded program.

    Such as: multi-line download Thunder, the server program, when the pressure testing

  4. Create a multi-threaded mode

    1. Thread class inheritance, implement the run method

  

 

 

 

 

  

 

 

           

 

 

    

// inherited Thread
public class MyThread extends Thread{
	// override the run () method
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+":"+i);
		}


public class Test {
	public static void main(String[] args) {
		MyThread thread=new MyThread();
	    thread.start();//启动线程	

  

 

    2.实现Runnable接口,重写run方法

 

 

 

  

 

 

 

 

//实现Runnable接口
public class MyRunnable implements Runnable{
	//重写run方法
	public void run() {
		for(int i=1;i<10;i++) {
		System.out.println(Thread.currentThread().getName()+":"+i);
	}

public class Test {
	public static void main(String[] args) {
		MyRunnable myRunnable=new MyRunnable();
		Thread myThread=new Thread (myRunnable);
		myThread.start();

  

 

 

 

    5.多线程的运行状态

  

 

     线程从创建、运行到结束总是处于下面五个状态之一:新建状态、就绪状态、运行状态、阻塞状态及死亡状态。

 

      1.新建状态

 

       当用new操作符创建一个线程时, 例如new Thread(r),线程还没有开始运行,此时线程处在新建状态。 当一个线程处于新生状态时,程序还没有开始运行线程中的代码

   2.就绪模式

  一个新创建的线程并不自动开始运行,要执行线程,必须调用线程的start()方法。当线程对象调用start()方法即启动了线程,start()方法创建线程运行的系统资源,并调度线程运行run()方法。当start()方法返回后,线程就处于就绪状态。处于就绪状态的线程并不一定立即运行run()方法,线程还必须同其他线程竞争CPU时间,只有获得CPU时间才可以运行线程。因为在单CPU的计算机系统中,不可能同时运行多个线程,一个时刻仅有一个线程处于运行状态。因此此时可能有多个线程处于就绪状态。对多个处于就绪状态的线程是由Java运行时系统的线程调度程序(thread scheduler)来调度的。

  3.运行模式

  当线程获得CPU时间后,它才进入运行状态,真正开始执行run()方法.

  4.阻塞模式

 

 

   线程运行过程中,可能由于各种原因进入阻塞状态:

        1>线程通过调用sleep方法进入睡眠状态;
        2>线程调用一个在I/O上被阻塞的操作,即该操作在输入输出操作完成之前不会返回到它的调用者;
        3>线程试图得到一个锁,而该锁正被其他线程持有;
        4>线程在等待某个触发条件;

  5.死亡模式

有两个原因会导致线程死亡:

   (1) run方法正常退出而自然死亡,
   (2)一个未捕获的异常终止了run方法而使线程猝死。
  为了确定线程在当前是否存活着(就是要么是可运行的,要么是被阻塞了),需要使用isAlive方法。如果是可运行或被阻塞,这个方法返回true; 如果线程仍旧是new状态且不是可运行的, 或者线程死亡了,则返回false.

Guess you like

Origin www.cnblogs.com/wangyangzhao/p/12168664.html