java code to achieve backup and resume database

2023-01-25   ES  

  • In the operating system, threads can be divided into priority, and those with higher priority will get more resources of the CPU, that is, the tasks in the high -priority threads will be given priority.
  • By setting the priority to help the “thread plan period” determine which thread to choose the next time to give priority execution

    The priority of the

  • thread is divided into 10 levels of 1 ~ 10. If you set it greater than 10 or less than 1, JDK will throw an abnormal Throw New Illegalargumentexception ()

/**
     * The minimum priority that a thread can have.
     */
    public final static int MIN_PRIORITY = 1; // minimum priority

   /**
     * The default priority that is assigned to a thread.
     */
    public final static int NORM_PRIORITY = 5; // Default priority

    /**
     * The maximum priority that a thread can have.
     */
    public final static int MAX_PRIORITY = 10; // Maximum priority

    public final void setPriority(int newPriority) {
    
        ThreadGroup g;
        checkAccess();
        // If the new priority is greater than 10 or less than 1 throw an exception
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
    
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
    
            if (newPriority > g.getMaxPriority()) {
    
                newPriority = g.getMaxPriority();
            }
            // Set priority
            setPriority0(priority = newPriority);
        }
    }

  • java thread’s priority is inherited. For example, thread A starts B, then B’s priority is the same as A’s
public class MyThread1 extends Thread {
    

    @Override
    public void run() {
    
        System.out.println("I am mythread1 My priority is:" + this.getPriority());
        MyThread2 thread2 = new MyThread2();
        thread2.start();
    }
}

class MyThread2 extends Thread {
    
    @Override
    public void run() {
    
        System.out.println("I am mythread1 my priority is:" + this.getPriority());
    }
}
public class Run {
    

    public static void main(String[] args) {
    
        System.out.println("I am main my priority is:" + Thread.currentThread().getPriority());
        Thread.currentThread().setPriority(9);
        System.out.println("I am the modified main priority:" + Thread.currentThread().getPriority());
        MyThread1 thread1 = new MyThread1();
        thread1.start();
    }
}
I am main my priority is:5I am the modified main priority. My priority is:9I am mythread1 My priority is:9I am mythread1 My priority is:9

Process finished with exit code 0

  • The so -called regularity is that high -priority threads are prioritized in most cases. But this does not mean that the high -priority thread is prioritized, and it will also be related to the order of the code. Unless there are two two, there are two two. The gap between the priority of individual threads is very large, and then it has nothing to do with the order of the code call.
  • For example, the following code
public class MyThread1 extends Thread {
    

    @Override
    public void run() {
    
        this.setPriority(1);
        System.out.println("I am mythread1 My priority is:" + this.getPriority());
        MyThread2 thread2 = new MyThread2();
        thread2.start();
        int i = 0;
        while (true) {
    
            i++;
            System.out.println("I am mythread1 =" + i);
        }
    }
}

class MyThread2 extends Thread {
    
    @Override
    public void run() {
    
        this.setPriority(9);
        System.out.println("I am mythread1 My priority is:" + this.getPriority());
        int i = 0;
        while (true) {
    
            i++;
            System.out.println("I am mythread2 =" + i);
        }
    }
}
  • Obviously starting first, but because the gap between priority is large, the execution of them has nothing to do with the order of code calling.
is mythread1=71681I am mythread1=71682I am mythread1=71683I am mythread1=71684I am mythread1=71685I am mythread2=241867I am mythread2=241868I am mythread2=241869I am mythread2=241870I am mythread2=241871I am mythread2=241872I am mythread2=241873I am mythread2=241874

  • The so -called randomness is that if the gap between the two threads is very small, each thread with a higher priority is performed first.
  • For example, the following code, even if you are higher than me, and you still start it first, but the execution result of the two is similar
public class MyThread1 extends Thread {
    

    @Override
    public void run() {
    
        this.setPriority(6);
        System.out.println("I am mythread1 My priority is:" + this.getPriority());
        MyThread2 thread2 = new MyThread2();
        thread2.start();
        int i = 0;
        while (true) {
    
            i++;
            System.out.println("I am mythread1 =" + i);
        }
    }
}

class MyThread2 extends Thread {
    
    @Override
    public void run() {
    
        this.setPriority(5);
        System.out.println("I am mythread1 My priority is:" + this.getPriority());
        int i = 0;
        while (true) {
    
            i++;
            System.out.println("I am mythread2 =" + i);
        }
    }
}
I am mythread1=163056I am mythread1=163057I am mythread1=163058I am mythread1=163059I am mythread1=163060I am mythread2=143756I am mythread2=143757I am mythread2=143758

  • In Java multi -threaded threads, one is the user thread, and the other is the guardian thread
  • Guardian thread is a special thread, which has the meaning of companionship. When there is no non -guarding thread in the thread, the guardian thread will automatically destroy. A guardian thread is all the nanny of all non -guardians in JVM
  • Daemon’s role is to facilitate the operation of other threads
public class DaemonTest extends Thread {
    

    @Override
    public void run() {
    
        try {
    
            int i = 0;
            while (true) {
    
                System.out.println("i=" + i++);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        }
    }
}
public class Run2 {
    

    public static void main(String[] args) throws InterruptedException {
    
        DaemonTest daemonTest = new DaemonTest();
        // Set daemontest as a guardian thread
        daemonTest.setDaemon(true);
        daemonTest.start();
        Thread.sleep(5000);
        System.out.println("The non -Guardian thread is executed, and the guardian thread should stop");
    }
}
i=0
i=1
i=2
i=3
i=4Non -Guardian thread is completed,Guardian thread should stop 

 Process Finished with EXIT CODE0

source

Related Posts

java code call sqlldr

Knowledge accumulation: Java SpringBoot Multi -threaded JOB transmitted Header solution to solve the problem of header loss, springcloud feign transmission Header

qt actual combat-start from main

Relations in early literature extraction papers to sort out, hurry up!

java code to achieve backup and resume database

Random Posts

Summary of Electric Tournament (4) -AD9834 of the chip summary of waveforms

ECharts add custom Geojson data implementation map display

0 ~ N-1 missing numbers

DNS server detailed explanation (port occupation and record type)

[Luoya Novice Village] P1980 counting problem Z