为了账号安全,请及时绑定邮箱和手机立即绑定

线程同步,synhronized,锁

线程同步,synhronized,锁

慕容708150 2019-03-20 09:15:40
要求主线程执行100次,子线程执行10次,主线程执行100次,子线程执行10次。。。循环代码如下:public class Test_02 {public static void main(String[] args){        MeThread me = new MeThread();    Thread t = new Thread(me);    t.start();    boolean flag = true;    int i = 1;    synchronized (me) {                while (flag) {            System.out.println(Thread.currentThread().getName() + i);            i++;            if (i % 100 == 0) {                try {                    Thread.currentThread().wait();                } catch (InterruptedException e) {                                        e.printStackTrace();                }finally {                    System.out.println("===========");                    me.notify();//Thread.currentThread().notifyAll();也不行                }            }        }    }}}class MeThread implements Runnable {@Overridepublic synchronized void run() {int i = 0;    boolean flag = true;    while (flag) {        System.out.println(Thread.currentThread().getName() + i);        i++;        if (i % 10 == 0) {            try {                Thread.currentThread().wait();            } catch (InterruptedException e) {                                e.printStackTrace();            }finally {                System.out.println("===========");                                this.notify();//Thread.currentThread().notifyAll();也不行            }        }    }}}
查看完整描述

2 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

public class Test_02 {


public static void main(String[] args) {


    MeThread me = new MeThread();

    Thread t = new Thread(me);

    t.start();


    boolean flag = true;

    //修改了初始值

    int i = 0;

    synchronized (me) {


        while (flag) {

            System.out.println(Thread.currentThread().getName() + i);

            i++;

            if (i % 100 == 0) {

                try {

                    //方便观察效果,睡眠1s

                    Thread.sleep(1000);

                    System.out.println("main===========");

                    //从finally代码块中前移到wait()方法前

                    me.notify();

                    //锁对象为me

                    me.wait();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

}

}

class MeThread implements Runnable {

@Override

public synchronized void run() {

int i = 0;


    boolean flag = true;

    while (flag) {

        System.out.println(Thread.currentThread().getName() + i);

        i++;

        if (i % 10 == 0) {

            try {

                //方便观察

                Thread.sleep(1000);

                System.out.println("MeThread===========");

                //同步方法锁对象为this,notify()移动到wait()方法前

                this.notify();

                //同步方法锁对象为this

                this.wait();

            } catch (InterruptedException e) {

                e.printStackTrace();

            } 

        }

    }

}

}


查看完整回答
反对 回复 2019-04-26
?
炎炎设计

TA贡献1808条经验 获得超4个赞

平时项目开发的时候这个多线程用在哪里?怎么使用?


查看完整回答
反对 回复 2019-04-26
  • 2 回答
  • 0 关注
  • 526 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号