Java multithreading study notes (3) sleep, yield, join

Thread sleep:

    Use Thread.sleep(long millis) to make the thread sleep, that is, to suspend the thread that is being executed, and give the CPU to other threads to execute.
Examples:

public class Example06 {
    
    
    public static void main(String[] args) {
    
    
        SleepThread sleepThread = new SleepThread();
        new Thread(sleepThread,"线程A").start();
        for (int i = 0; i <= 5; i++) {
    
    
            if (i == 5){
    
    //i=5时,休眠2秒
                try {
    
    
                    Thread.sleep(2000);
                }catch (Exception e){
    
    
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "输出了" + i);
        }
    }
}
//SleepThread实现Runnable接口
class SleepThread implements Runnable {
    
    
    public void run() {
    
    
        for (int i = 0; i <= 5; i++) {
    
    
            try {
    
    
                if (i == 5) {
    
    //i=5时,休眠2秒
                    Thread.sleep(2000);
                }
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "输出了" + i);
        }
    }
}

operation result:
Insert picture description here

Thread concessions:

    Thread concession can be achieved through the yield() method, which is similar to the sleep() method in that it can pause the running thread. The difference is that the yield() method does not block the thread, but only converts the thread to the ready state, allowing the system scheduler to schedule it again. When the thread calls the yield() method, only threads with the same or higher priority as the current thread will get the chance to execute.
Examples:

public class Example07 {
    
    
    public static void main(String[] args) {
    
    
        yieldThread yieldThread1 = new yieldThread();//创建实例
        new Thread(yieldThread1,"线程A").start();//启动线程
        yieldThread yieldThread2 = new yieldThread();//创建实例
        new Thread(yieldThread2,"线程B").start();//启动线程
    }
}
class yieldThread implements Runnable{
    
    
    public void run() {
    
    
        for (int i = 0; i <= 5; i++ ){
    
    
            System.out.println(Thread.currentThread().getName() + "---" + i);
            if (i == 4){
    
    
                System.out.print("线程让步了:");
                Thread.yield();//线程执行到这会让步
            }
        }
    }
}

operation result:
Insert picture description here

Thread jump:

    When the join() method of another thread is called in a thread, the calling thread will be blocked, and it will not run until the added thread finishes executing.
Examples:

public class Example08 {
    
    
    public static void main(String[] args) throws Exception {
    
    
        Thread a = new Thread(new JoinThread(), "线程A");
        a.start();
        for (int i = 0; i <= 5; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "输出" + i);
            if (i == 2){
    
    
                a.join();//调用join()方法
            }
            Thread.sleep(1000);//休眠1秒中
        }
    }
}
class JoinThread implements Runnable {
    
    
    public void run() {
    
    
        for (int i = 0; i <= 5; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "输出" + i);
            try {
    
    
                Thread.sleep(1000);
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

Operation result:
Insert picture description here
Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109499171