当前位置:网站首页>Common thread scheduling methods

Common thread scheduling methods

2022-06-22 10:51:00 It midsummer fruit

Catalog

One Thread waiting

Two Thread notification

3、 ... and Thread to sleep

Four Request surrender CPU Executive power

5、 ... and Thread the interrupt


One Thread waiting

1 wait(): When a thread calls wait() After the method , This thread will be blocked and suspended , It will not return until the following conditions occur

1) The thread called notify() perhaps notifyAll() Wake up the way

2) Another thread called the thread's interrupt() Interrupt method , Thread throws InterruptedException Exception return .

2 wait(long timeout): This method is better than wait() Method has one more timeout parameter timeout, If the thread calls this method , Not in the specified timeout Wake up in time , Then this method will return because of timeout .

3 wait(long timeout,int nacos): and wait(long timeout) similar , It also has a timeout wait, It's also called internally wait(long timeout) Method

4 join(): Let the parent thread wait for the child thread to finish before continuing execution , That is, when we call a thread's join() When the method is used , This method suspends the calling thread , Until the called thread ends execution , The calling thread will continue to execute .

Two Thread notification

1 notify(): When a thread calls notify() After the method , Will wake up a call on the lock resource wait() Thread suspended after method . A lock resource may have multiple threads waiting , Which waiting thread to wake up is random .

2 notifyAll():notify() Is to wake up a waiting thread , and notify() Method will wake up all the resources on the lock due to the call wait() Method .

3、 ... and Thread to sleep

1 sleep(long millis):sleep The method is Thread Class static methods , When an executing thread calls sleep() After the method , This thread will temporarily give up the specified time CPU Executive power , But the monitor resources owned by the thread , For example, the lock is still held . When the designated sleep time is up sleep() Method will return normally , Then participate in the call CPU The scheduling , Get CPU Resource will continue to execute .

Four Request surrender CPU Executive power

1 yield(): yes Thread Static methods in a class , When a thread calls yield After the method , In fact, it tells the thread scheduler that the current thread requests to give up its own CPU, Let yourself or another thread run , But the thread scheduler can unconditionally ignore this hint .

Make current thread from execution state ( Running state ) Become executable ( Ready state ).CPU Will choose from a number of executable states . That is to say, calling yield() The thread of the method may be executed again , It does not mean that other threads will be executed and this thread will not be executed next time , Depends on the choice of the thread scheduler .

demo demonstration :

public class YieldDemo extends Thread{

    public YieldDemo(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(this.getName()+"-----"+i);
            if (i == 5 ) {
                System.out.println(""+this.getName()+"-- Request the thread scheduler to give up CPU The execution right is given to other threads or executed by itself ");
                Thread.yield();
            }
        }
    }

    public static void main(String[] args) {
         new YieldDemo(" Zhang San ").start();
         new YieldDemo(" Li Si ").start();

    }
}

Results, :

5、 ... and Thread the interrupt

1 void interrupt(): How to interrupt a thread , The interrupt flag of this thread will be set to true, The result of the interrupt thread is death , Or wait for a new task or continue to run the next step , It depends on the program itself . From time to time, the thread will detect that the interrupt flag bit is true, To determine whether the thread should be interrupted .

for example : When a thread A Runtime , Threads B You can call interrupt() Method to set the interrupt flag of the thread to true And return immediately . Setting a sign is just setting a sign , Threads A Actually not interrupted , Will continue down .

2 boolean isInterrupted(): Determine whether a thread has sent an interrupt request , That is, whether the current thread is interrupted ,isInterrupted() The interrupt flag bit will not be cleared .

3 boolean interrupted(): Detect whether the current thread is interrupted , And isInterrupted The difference is , If the method finds that the current thread is interrupted , The interrupt flag is cleared .isInterrupted() Will clear interrupt flag bit , Set it to false.

 

原网站

版权声明
本文为[It midsummer fruit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221032376409.html