线程设计模式

1.固定运行顺序

(1)wait-notify

public class Asyn {
    //1,2
    static Object obj=new Object();
    static int flag=0;
    public static void main(String[] args) throws InterruptedException {
       Thread t1= new Thread(()->{
            synchronized (obj) {
                System.out.println("1");
                flag=1;
                obj.notifyAll();
            }
        },"t1");
       Thread t2= new Thread(()->{
            synchronized (obj){
                while (flag!=1){
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                flag=0;
                System.out.println("2");
            }
        },"t2");
       t2.start();
       t1.start();
    }

(2)await-signal

 @Test
    public void awaitsingnal(){
        ReentrantLock reentrantLock=new ReentrantLock();
        Condition condition=reentrantLock.newCondition();
        Thread t2=new Thread(()->{
            try {
                reentrantLock.lock();
                while (flag!=1){
                    condition.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                reentrantLock.unlock();
            }
            System.out.println("2");
        },"t2");
       Thread t1= new Thread(()->{
            try {
                reentrantLock.lock();
                System.out.println("1");
                flag=1;
                condition.signal();
            }finally {
                reentrantLock.unlock();
            }
        },"t1");
       t1.start();
       t2.start();
    }

(3)part-unpart

public void testpart(){
        Thread t2=new Thread(()->{
            LockSupport.park();
            System.out.println("2");
        },"t2");
       Thread t1= new Thread(()->{
            System.out.println("1");
           LockSupport.unpark(t2);
        },"t1");
       t2.start();
       t1.start();
    }

2.交替输出

public class Alternate {
    static Thread t1,t2,t3;
//wait-notify
    public static void main(String[] args) throws InterruptedException {
        Lock lock=new Lock(3);
        Thread A=new Thread(()->{
            try {
                lock.print("a",1,2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A");
        Thread B=new Thread(()->{
            try {
                lock.print("b",2,3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B");
        Thread C=new Thread(()->{
            try {
                lock.print("c",3,1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"C");
        C.start();
        B.start();
        A.start();
    }
//await-signal
    @Test
    public void testre() throws InterruptedException {
       AwaitSignal awaitSignal=new AwaitSignal(3);
        Condition A=awaitSignal.newCondition();
        Condition B=awaitSignal.newCondition();
        Condition C=awaitSignal.newCondition();
        new Thread(()->{
            awaitSignal.print("a",A,B);
        }).start();
        new Thread(()->{
            awaitSignal.print("b",B,C);
        }).start();
        new Thread(()->{
            awaitSignal.print("c",C,A);
        }).start();
        Thread.sleep(1000);
        awaitSignal.lock();
        try{
            A.signal();
        }finally {
            awaitSignal.unlock();
        }
    }
//part-unpart
    @Test
    public void testPartUnpart() throws InterruptedException {
        PartUnpart partUnpart=new PartUnpart(2);
        t1=new Thread(()->{
           partUnpart.print("a",t2);
        });
        t2=new Thread(()->{
            partUnpart.print("b",t3);
        });
        t3=new Thread(()->{
            partUnpart.print("c",t1);
        });
        t3.start();
        t1.start();
        t2.start();
        Thread.sleep(500);
        LockSupport.unpark(t1);
    }
}
class PartUnpart{
    private int LoopNumber;

    public PartUnpart(int loopNumber) {
        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);
        }
    }
}
class AwaitSignal extends ReentrantLock{
    private int LoopNumber;

    public AwaitSignal(int loopNumber) {
        LoopNumber = loopNumber;
    }
    public void print(String str,Condition con,Condition next){
        for (int i = 0; i < LoopNumber; i++) {
            lock();
            try {
                con.await();
                System.out.print(str);
                next.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                unlock();
            }
        }
    }
}
class Lock{
    private int count=1;
    private int loopNumber;

    public Lock(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public int getCount() {
        return count;
    }

    public  void print(String str,int now,int next) throws InterruptedException {
        for (int i = 0; i <loopNumber ; i++) {
            synchronized (this) {
                while (count!=now){
                    wait();
                }
                System.out.print(str);
                count=next;
                notifyAll();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值