侧边栏壁纸
  • 累计撰写 120 篇文章
  • 累计创建 281 个标签
  • 累计收到 11 条评论
标签搜索
隐藏侧边栏

java线程间通讯

骐骏
2017-03-24 / 0 评论 / 0 点赞 / 878 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2017-03-24,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

一、 wait,notify方法使用示例:

package com.fullstacker.study.course.concurrent;

/**
 * 线程间通讯
 *
 * @author xingguishuai
 * @create 2017-03-17-13:25
 **/
public class ThreadComunicate {

    public static void main(String[] args) throws InterruptedException {
        final Object lock = new Object();
        int count = 0;

        Thread thread = new Thread(new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while (true) {
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + ":子线程:"+count);

                        count++;
                        if(count==5){
                                lock.notify();
//                            try {
//                                lock.wait();
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
                        }
                        if(count > 10){
                            break;
                        }
                    }
                }

            }
        });
        thread.start();
        synchronized(lock){
            while (true){
                System.out.println(Thread.currentThread().getName()+":"+count);
                count++;
                if(count==5){
                    lock.wait();
                }
                if(count>10){
                    break;
                }
            }
        }
    }

}

  1. wait,notify方法都属于Object对象。
  2. 调用wait方法后,线程释放资源,等待notify方法唤醒

二、 join方法

package com.fullstacker.study.course.concurrent;

/**
 * join方法使用示例
 *
 * @author xingguishuai
 * @create 2017-03-24-14:22
 **/
public class ThreadJoin {
    /**
    * <p>功能描述:在执行thread2过程中,调用thread1.join()方法后,thread2线程暂停,而后thread1继续执行,待thread1执行完毕thread2继续执行</p>
    * @return
    * @param
    * @author xingguishuai
    * @Date 2017-03-24 14:27
    * @since 1.0
    */
    public static void main(String[] args){

        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i =0;
                while (i<10){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                    i++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread1.start();
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i =0;
                while (i<10){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                    i++;
                    try {
                        thread1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread2.start();
    }

}

  1. 在执行thread2过程中,调用thread1.join()方法后,thread2线程暂停,而后thread1继续执行,待thread1执行完毕thread2继续执行
  2. join方法可以将将两个交叉执行线程转换为线性执行,如例所示:在调用thread1.join()后,thread2线程就要等待thread1执行完毕后再继续执行。
0

评论区