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();
        }
    }
}

你可能感兴趣的:(java多线程/IO)