1. Synchronized方式

package com.hourui.validate.Test3;

public class TestSynchronizedWaitNotify {
    public static void main(String[] args) {
        final Object o = new Object();

        char[] a1 = "1234567".toCharArray();
        char[] ac = "ABCDEFG".toCharArray();

        new Thread(() -> {
            synchronized (o) {
                for(char c : a1) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait(); //让出锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();  //必须,否则无法停止程序
            }
        },"t1").start();

        new Thread(() -> {
            synchronized (o) {
                for(char c : ac) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();  //让出锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify(); //必须,否则无法停止程序
            }
        },"t2").start();
    }
}

 

2.Condition方式

package com.hourui.validate.Test3;

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

public class TestCondition {
    private static int num = 1;

    public static void main(String[ ]args) {
        ReentrantLock lock = new ReentrantLock();
        Condition c1 = lock.newCondition();
        Condition c2 = lock.newCondition();

        char[] a1 = "1234567".toCharArray();
        char[] ac = "ABCDEFG".toCharArray();

        new Thread(() ->{
            try {
                lock.lock();
                for(char c :a1) {
                    while (num != 1) {
                        try {
                            c1.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.print(c);
                    num = 2;
                    c2.signal();
                }

            } finally {
                lock.unlock();
            }
        },"t1").start();


        new Thread(() ->{
            try {
                lock.lock();
                for(char c :ac) {
                    while (num != 2) {
                        try {
                            c2.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.print(c);
                    num = 1;
                    c1.signal();
                }

            } finally {
                lock.unlock();
            }
        },"t2").start();

    }

}

 

3.CAS方式

package com.hourui.validate.Test3;

public class TestCAS {
    enum ReadyTORun{T1, T2}
    static volatile ReadyTORun r  = ReadyTORun.T1; //思考为什么必须是volatile

    public static void main(String []args) {

        char[] a1 ="1234567".toCharArray();
        char[] ac = "ABCDEFG".toCharArray();

        new Thread(() -> {
            for(char c : a1) {
                while(r != ReadyTORun.T1) {

                }
                System.out.print(c);
                r = ReadyTORun.T2;

            }
        },"t1").start();

        new Thread(() -> {
            for(char c : ac) {
                while(r != ReadyTORun.T2) {

                }
                System.out.print(c);
                r = ReadyTORun.T1;

            }
        },"t2").start();
    }
}

 

4.BlockingQueue方式

package com.hourui.validate.Test3;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TestBlockingQueue {
    static BlockingQueue<String> q1 = new ArrayBlockingQueue<String>(1);
    static BlockingQueue<String> q2 = new ArrayBlockingQueue<String>(1);

    public static void main(String[ ]args) {
        char[] a1 = "1234567".toCharArray();
        char[] a2 = "ABCDEFG".toCharArray();

        new Thread(() -> {
            for(char c : a1) {
                System.out.print(c);
                try {
                    q1.put("ok");
                    q2.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"t1").start();

        new Thread(() -> {
            for(char c : a2) {

                try {
                    q1.take();
                    System.out.print(c);
                    q2.put("ok");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"t2").start();
    }
}

 

5.LockSupport方式

package com.hourui.validate.Test3;

import java.util.concurrent.locks.LockSupport;

public class TestLockSupport {

    static Thread t1 = null, t2 = null;

    public static void main(String[ ]args) {
        char[] a1 = "1234567".toCharArray();
        char[] ac = "ABCDEFG".toCharArray();
        t1 = new Thread(() -> {
            for(char c : a1) {
                System.out.print(c );
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        },"t1");

        t2 = new Thread(() -> {
            for(char c : ac) {
                LockSupport.park();
                System.out.print(c );
                LockSupport.unpark(t1);

            }
        },"t1");

        t1.start();
        t2.start();
    }
}

 

posted on 2020-08-18 00:17  从精通到陌生  阅读(241)  评论(0编辑  收藏  举报