Java day 23 summary

Keywords: Java Back-end

1. Basic concepts: program, process and thread

A program is a set of instructions written in a certain language to complete a specific task. It refers to a piece of static code, static object.

A process is an execution process of a program, or a running program. It is a dynamic process: it has its own process of emergence, existence and extinction—— life cycle

Such as QQ in operation and MP3 player in operation
The program is static and the process is dynamic
Process is the unit of resource allocation. The system will allocate different memory areas for each process at run time

Thread, a process can be further refined into a thread, which is an execution path within a program.

If a process executes multiple threads in parallel at the same time, it supports multithreading
As a unit of scheduling and execution, each thread has an independent running stack and program counter (pc), and the overhead of thread switching is small

Sketch Map:

2. Creation and use of threads

  The JVM of the Java language allows programs to run multiple threads, which is embodied by the java.lang.Thread class.  

Properties of Thread class
Each Thread completes the operation through the run() method of a specific Thread object,
The body of the run() method is often referred to as the thread body
Start the Thread through the start() method of the Thread object instead of calling the run() method directly

2.1 mode 1: Tread type

1. Define subclasses to inherit Thread class.
2. Override the run method in the Thread class in the subclass.
3. Create a Thread subclass object, that is, create a Thread object.
4. Call the thread object start method: start the thread and call the run method.

public class MyThread extends Thread {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"1111");
		}
	}
}

Operation mode:

public class MyThreadRun {
	public static void main(String[] args) throws InterruptedException {
		MyThread my=new MyThread();
		MyThread2 my2=new MyThread2();
		my.start();
		my2.start();
		System.out.println(Thread.activeCount());
		while(true) {
			Thread.sleep(2000);
			System.out.println(Thread.currentThread().getName());
		}	
	}
}

2.2 mode 2: Runnable interface

1. Define subclasses and implement Runnable interface.
2. Override the run method in the Runnable interface in the subclass.
3. Create a Thread object through the Thread class parameter constructor.
4. Pass the subclass object of Runnable interface as the actual parameter to the constructor of Thread class.
5. Call the start method of Thread class: start the Thread and call the run method of Runnable subclass interface.

public class Demo2 implements Runnable{
	public void run() {
		  int i=0;
		  while(i<100) {
			  System.out.println(Thread.currentThread().getName()+" running "+i);
			  i++;
		  }
	}
}

Operation mode:

public static void main(String[] args) {
		Thread thread1 = new Thread(new Demo2());
		Thread thread2 = new Thread(new Demo2());
		thread1.start();
		thread2.start();
	}

3. Common methods

1.getName(): get the name of the current thread
2.setName(): sets the name of the thread. The default is thread-0, and thread-1 is incremented in turn
3.setPriority(): set priority. java has 10 priorities, 1-10
      It is represented by three constants in the Thread class
            public final static int MIN_PRIORITY = 1;
            public final static int NORM_PRIORITY = 5;
            public final static int MAX_PRIORITY = 10;
      The default subclass inherits the priority of the parent class, while the priority of Thread is 5
4.static currentThread(): get the current thread object, which class to write in, and which thread class object to get
5.static sleep(): put the current thread into sleep state. The parameter is the number of milliseconds of sleep. The class in which it is written depends on which thread class it sleeps
​​​​​​​6.   interrupt(): forcibly wakes up a sleeping thread and throws an exception

Posted by cashflowtips on Thu, 28 Oct 2021 10:33:43 -0700