java间线程通信的几种方式(II)

1.如何让两个线程嫩能够交叉执行
要让线程能够交叉执行,需要用到锁。看如下代码:

package Test;/**
/**
 * @author Administrator wangtao
 * @createdate 2017-10-10
 */
public class demo3 {
    public static void  main(String[] args ){
        Object lock = new Object();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println("A  1");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("A  2");
                    System.out.println("A  3");
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println("B 1");
                    System.out.println("B 2");
                    System.out.println("B 3");
                    lock.notify();
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}

线程1先获得锁,执行一段任务之后,调用wait()方法,让线程释放锁,线程2获得锁,执行任务。等到线程2执行完成后,通过notify()。唤醒线程。

2.四个线程 A B C D,其中 D 要等到 A B C 全执行完毕后才执行,而且 A B C 是同步运行的

1.创建一个计数器,设置初始值,CountdownLatch countDownLatch = new CountDownLatch(2);
2.在 等待线程 里调用 countDownLatch.await() 方法,进入等待状态,直到计数值变成 0;
3.在 其他线程 里,调用 countDownLatch.countDown() 方法,该方法会将计数值减小 1;
4.当 其他线程 的 countDown() 方法把计数值变成 0 时,等待线程 里的 countDownLatch.await() 立即退出,继续执行下面的代码。

package Test;/**
import java.util.concurrent.CountDownLatch;
/**
 * @author Administrator wangtao
 * @createdate 2017-10-10
 */
public class demo4 {
    public static void main(String[] args){
        int worker =3;
        CountDownLatch countDownLatch = new CountDownLatch(worker);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("D is waiting for other three threads");
                try {
                    countDownLatch.await();//线程处于等待状态,直到计数器等于0线程会继续执行
                    System.out.println("All done, D starts working");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        for(char threadName='A';threadName <= 'C';threadName++){
            final String tN = String.valueOf(threadName);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(tN + "is working");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(tN +"finished");
                    countDownLatch.countDown();
                }
            }).start();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值