Java教程

多线程的顺序执行

本文主要是介绍多线程的顺序执行,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

wait&notify实现多线程的顺序执行

package com.cxf.test;

public class test5 {
    public static void main(String[] args) {
        WaitNofity wn = new WaitNofity(5, 1);
        new Thread(()->{
          wn.print("a",1,2);
        }).start();
        new Thread(()->{
            wn.print("b",2,3);
        }).start();
        new Thread(()->{
            wn.print("c",3,1);
        }).start();
    }
}
class WaitNofity{
    private Integer loopNumber;
    private Integer flag;

    public WaitNofity(Integer loopNumber, Integer flag) {
        this.loopNumber = loopNumber;
        this.flag = flag;
    }

    public WaitNofity() {
    }
    public  void print(String str,Integer waitFlag,Integer nextFlag){
        for (int i = 0; i <loopNumber ; i++) {
            synchronized (this){
                while(waitFlag!=flag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(str);
                flag=nextFlag;
                this.notifyAll();
            }
        }
    }
}

ReentrantLock的条件变量实现多线程的顺序执行

package com.cxf.test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class test6 {
    public static void main(String[] args) {
        AwaitSinal awaitSinal = new AwaitSinal(5);
        Condition A = awaitSinal.newCondition();
        Condition B = awaitSinal.newCondition();
        Condition C = awaitSinal.newCondition();
        new Thread(() -> awaitSinal.print("a",A,B)).start();
        new Thread(() -> awaitSinal.print("b",B,C)).start();
        new Thread(() -> awaitSinal.print("c",C,A)).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        awaitSinal.lock();
        try {
            A.signalAll();
        } finally {
            awaitSinal.unlock();
        }
    }
}
class AwaitSinal extends ReentrantLock{
    private Integer loopNumber;
    public AwaitSinal(Integer loopNumber){
        this.loopNumber=loopNumber;
    }
    public void print(String str, Condition current,Condition next){
        for (int i = 0; i <loopNumber ; i++) {
            lock();
            try {
                current.await();
                System.out.print(str);
                next.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                unlock();
            }
        }
    }
}

park&unpark实现多线程的顺序执行

package com.cxf.test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class test7 {
    static  Thread t1;
    static  Thread t2;
    static  Thread t3;

    public static void main(String[] args) {
        ParkUnpark parkUnpark = new ParkUnpark(5);
        t1=new Thread(() -> parkUnpark.print("a",t2));
        t2=new Thread(() -> parkUnpark.print("b",t3));
        t3=new Thread(() -> parkUnpark.print("c",t1));
        t1.start();
        t2.start();
        t3.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LockSupport.unpark(t1);
    }

}
class ParkUnpark{
    private  int loopNumber;
    public  ParkUnpark(int loopNumber){
        this.loopNumber=loopNumber;
    }
    public void print(String str,Thread next){
        for (int i=0;i<loopNumber;i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);
        }
    }
}

这篇关于多线程的顺序执行的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!