多线程实现:两个字符串,启动2个线程,交替输出

synchronized和ReentrantLock,LockSuport三种方法实现

package c_26interview;

/**

 * 需求給两个字符串。启动2个线程,交替输出

 * synchronized加到锁对象,通过锁对象的notify()和wait()方法实现唤醒和等待

 */

public class T02_syn_wait_notify {

    public static void main(String[] args){

        char[] c1 = "1234".toCharArray();

        char[] c2 = "abcd".toCharArray();

       final Object o = new Object();

        new Thread(()->{

            synchronized(o){

                for (char c : c1) {

                    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 : c2) {

                    try {

                        o.wait();

                        System.out.print(c);

                        o.notify();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                    o.notify();//必须,否则无法停止程序

                }

            }

        },"t2").start();

    }

}

package c_26interview;

import lock.ReentrantLock;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

/**

 * lock.newCondition()创建2个队列,通过condition的signal()唤醒,await等待线程

 */

public class T05_lock_condition {

    public static void main(String[] srg) {

        Lock lock = new java.util.concurrent.locks.ReentrantLock();

        Condition condition1 = lock.newCondition();

        Condition condition2 = lock.newCondition();

        char[] c1 = "1234".toCharArray();

        char[] c2 = "abcd".toCharArray();

        new Thread(()->{

            lock.lock();

            try {

                for (char c : c1) {

                    System.out.print(c);

                    condition2.signal();//唤醒第二个线程

                    condition1.await();//第一个线程设置等待

                }

                condition1.signal();//设置第一个线程等待

            } catch (InterruptedException e) {

                e.printStackTrace();

            }finally{

                lock.unlock();

            }

        }).start();

       new Thread(()->{

           lock.lock();

           try {

               for (char c : c2) {

                   System.out.print(c);

                   condition1.signal();

                   condition2.await();

               }

               condition2.signal();

           } catch (InterruptedException e) {

               e.printStackTrace();

           }finally{

               lock.unlock();

           }

       }).start();

    }

}

package c_26interview;

import java.util.concurrent.locks.LockSupport;

/**

 * 需求給两个字符串。启动2个线程,交替输出

 * 利用LockSupport.park()暫停和LockSupport.unPark()重啟

 */

public class T08_lockSupport {

   static Thread t1 = null;

   static Thread t2 = null;

    public static void main(String[] args){

        char[] c1="ABCD".toCharArray();

        char[] c2="1234".toCharArray();

        t1=new Thread(()->{

            for(int i=0; i<c1.length;i++){

                System.out.print(c1[i]);

                LockSupport.unpark(t2);

                LockSupport.park();

            }

        },"t1");

       t2= new Thread(()->{

            for(int j=0; j<c2.length;j++){

                LockSupport.park();

                System.out.print(c2[j]);

                LockSupport.unpark(t1);

            }

        },"t2");

       t1.start();

       t2.start();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值