"Android Advanced Light" reading notes

Four, multi-threaded programming (4.1)

  1. The basic unit of operating system management: process
  2. The smallest unit of operating system scheduling: thread (lightweight process)
  3. Multiple threads can be created in a process, and these threads have their own counters, stacks and local variables, and can also access shared memory variables
  4. The significance of multi-threading: reduce program response time, make the program have better interactivity, reduce the overhead of thread creation and switching, improve the utilization rate of multi-core CPU, avoid resource waste, simplify the program structure, and be easy to maintain
  5. Thread state:
  • New: Create state, the start method has not been called yet
  • Runnable : runnable state, the start method is called, it may be running or not running, depending on the time provided by the operating system to the thread
  • Blocked : blocked state, temporarily inactive
  • Waiting: waiting state, temporarily inactive and not running any code, consumes the least resources
  • Timed Waiting: Timeout waiting, can return within the specified time
  • Terminated: terminated state, the thread execution is completed, it may exit normally after the execution of the run method, or it may terminate due to an uncaught exception

Thread is essentially an instance of the Runnable interface, and there are three ways to implement multithreading:

1. Inherit the Thread class and rewrite the run method

public class TestThread extends Thread{
    public void run(){
        System.out.println("hello world");
    }
    public static void main(String[] args){
        Thread mThread = new TestThread();
        mThread.start();
    }
}

 2. Implement the Runnable interface and implement the run method of the interface

public class TestRunnable implements Runnable{
    public void run(){
        System.out.println("hello world");
    }
}

public class TestRunnable {
    public static void main(String[] args) {
        TestRunnable mTestRunnable = new TestRunnable();
        Thread mThread = new Thread(mTestRunnable);
        mThread.start();
    }
}

3. Callable interface, rewrite the call method: similar to method 2 but provides more powerful functions than method 2, such as providing a return value after the task is accepted, and the call method can throw an exception

public class TestCallable {
    public static class MyTestCallable implements Callable {
        public String call() throws Exception {
            return "hello world";
        }
    }
    public static void main(String[] args){
        MyTestCallable mMyTestCallable =new MyTestCallable();
        ExcecutorService mExcecutorService = Excecutors.newSingleThreadPool();
        Future mfuture = mExecutorService.submit(mMyTestCallable);
        try {
            System.out.println(mfuture.get());
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}

Thread interruption: You can call Thread.currentThread().isInterrupted() to see if the thread is set, and you can also call Thread.interrupted() to reset the interrupt flag, but the interrupt state cannot be detected when the thread is blocked . If the interrupt flag bit =true, an InterruptedException exception will be thrown, and the interrupt flag bit of the thread will be reset to false before the exception is thrown , and the exception cannot be caught in the underlying code without processing :

void myTask() {
    
    try{
        sleep(50)
    } catch(InterruptedExcetion e){

    }

}

There are two ways to handle it:

1. Do not catch such an exception, let the method throw it directly, so that the caller can catch this exception:

void myTask() throws InterruptedException{
    sleep(50)
}

2. Call Thread.interrupt() in the catch to set the interrupt status, and let the outside world judge whether to terminate the thread through Thread.isInterrupted:

void myTask() {
    
    try{
        sleep(50)
    } catch(InterruptedExcetion e){
        Thread.currentThread.interrupt();
    }

}

 

 

Guess you like

Origin blog.csdn.net/LYFly0016/article/details/130702584