Process and thread

1. Process

The running program occupies space in memory

1.1 features

1) Independence

2) Dynamic program is static and process is dynamic

3) Concurrency

1.2 parallelism and concurrency

High concurrency: multiple processes seize public resources

Parallelism: multiple CPU s process different processes at the same time

2. Thread

The smallest unit in which the system can perform operation scheduling, which is included in the process, is the actual operation unit of the process

There are separate areas and public areas

2.1 features

1) Randomness} at the same time, only one thread is executing

2) CPU time-sharing scheduling

We can't control which threads to execute. The underlying OS has its own rules

① FCFS first come first served

② SJS short service algorithm

2.2 thread status

Basic model: three state model

Ready (operational) status, execution (operational) status, blocking status

Only ready status can be transferred to run (selected by CPU)

Five state model

PCB? Record the basic situation and activity process of the thread

2.3 multi Thread code creation method 1: inherit Thread

--extends Thread

be careful:

1) By inheritance -- extends Thread

2) The business of the custom thread class needs to be written in the rewritten run()

3) New corresponds to the new state of the thread

4) If you just call run(), the task of one thread will be executed first, and then the task of another thread will be executed. It will not be started in the way of multithreading at all, so there is no effect of multithreading preempting resources

5) The state corresponding to start() is the ready state of the thread. The execution time depends on when it is selected by the operating system

6) When we call start() to start the thread, the underlying virtual opportunity automatically calls run() to execute our business

7) If you want the program to have the effect of multithreading, you need to create multiple thread objects and call start() to add the thread to the ready queue. Calling run() can only be used as an ordinary method call without the effect of multithreading

8) The effect of thread execution is random. How to execute depends on CPU scheduling and time slice allocation

9)

package cn.tedu.map.thread;
//This class is used for multi-threaded programming implementation scheme 1
public class TestThread1 {
    public static void main(String[] args) {


//        //4. Create thread object
//        /*5.new corresponds to the new state of the thread*/
//        /*6. If you only call run() through two thread objects, the task of one thread will be executed first,
//        When another thread is executed, it will not be started in a multi-threaded manner at all, so there is no effect of multi-threaded preemption of resources*/
//        MyThread t=new MyThread();
        t.run();
//        //5. To simulate multithreading, you need to enable at least two threads. If you start only one thread, it is a single thread
//        MyThread t2=new MyThread();/*5.new corresponds to the new state of the thread*/
        t2.run();
//        /*7. The state corresponding to start () is the ready state of the thread. The execution time depends on when it is selected by the operating system*/
//        /*8. When we call start() to start the thread, the underlying virtual opportunity automatically calls run() to execute our business*/
//        /*9. If you want the program to have the effect of multithreading, you need to create multiple thread objects and call start()
//        *The thread can only be added to the ready queue. Calling run() can only be used as an ordinary method call, without the effect of multithreading*/
//        /*10. The effect of thread execution is random, and the execution effects of t1, t2 and t3 are uncontrollable,
//        How to execute depends on CPU scheduling and time slice allocation, which we can't control*/
//        t.start();
//        t2.start();
//        //7. Create a custom thread object and start
//        MyThread t3=new MyThread("Dalian");
//        t3.start();

    }
}



1.Custom multithreading class
///*1. Method 1: through inheritance -- extends Thread*/
//class MyThread extends Thread{
//    /*11. In order to modify the name of a thread, instead of using the default name assigned by the system, you can provide a parameter structure
//    *Note: the function of naming a thread is actually completed by the parent class, and the constructor of the child class only calls super(name)*/
//    //6. Add parameterized and nonparametric constructs of custom thread classes
//    public MyThread(String name) {
//        super(name);
//    }
//    public MyThread(){
//        super();
//    }
//
//    //2. Rewrite run(), and write our own business in run()
//    @Override
//    public void run() {
//        /*2. The business of the custom thread class needs to be written in the rewritten run()
//        * 3.super.run() means that the business of the parent class is called, so we don't need to comment it out*/
        super.run();
//        //3. Write your own business: output the name of the thread currently executing 10 times
//        for (int i = 0; i < 10; i++) {
//            /*4.getName() means that you can get the name of the thread currently executing
//            *Since the subclass inherits the Thread class, you can use this method directly*/
//            System.out.println(i+"="+getName());
//        }
//    }
//}

2.4 multithreading implementation scheme 2: implement Runnable interface

In the Runnable interface, no redundant methods are provided, only an abstract run()

Thread.currentThread() -- get the thread object currently executing, because the thread is Java Lang package, so you can use it directly without importing the package

And, thread Currentthread () is a static method, so it can be called directly through the class name

package cn.tedu.map.thread;
//This class is used for multithreading implementation scheme 2
public class TestThread2 {
    public static void main(String[] args) {
        //4. Create the target business object -- including the business (what to do)
        /*The target business object only needs to be created once, which is similar to the system publishing the same task target to all players*/
        MyRunnable target=new MyRunnable();
        //5. Simulate multithreading and create multiple thread objects
        /*Reason: neither Runnable nor MyRunnable has start(),
        Therefore, it is necessary to establish a relationship between the target business object created by the interface implementation class and the thread class,
        Only after the relationship is established can the Thread be started in a multi-threaded manner by using the start() of the Thread class*/
        Thread a=new Thread(target);
        Thread a2=new Thread(target);
        Thread a3=new Thread(target);
        Thread a4=new Thread(target);
        a.start();
        a2.start();
        a3.start();
        a4.start();
    }
}
//1. Custom multithreading class
/*1.Multithreading implementation scheme 2: Implementation -- implements Runnable*/
class MyRunnable implements Runnable{

    //2. Write the custom business in run() to implement the methods not implemented in the interface
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            /*Problem: in the Runnable interface, no redundant methods are provided, only an abstract run()
            * Thread.currentThread()--Gets the thread object currently executing
            * Because Thread is Java Lang package, so you can use it directly without importing the package
            * And, thread Currentthread () is a static method, so it can be called directly through the class name
            * getName():Gets the name of the current thread*/
            System.out.println(i+"="+Thread.currentThread().getName());
        }
    }
}

Added by hoogeebear on Mon, 20 Dec 2021 08:30:15 +0200