java线程间通讯

一、 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执行完毕后再继续执行。

创作不易,转载请注明文章来源为www.full-satcker.com:邢桂帅 » java线程间通讯

赞 (1)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址