首页 新闻 会员 周边

线程同步,synhronized,锁

0
悬赏园豆:10 [已解决问题] 解决于 2019-04-19 16:26

要求主线程执行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 {
@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.currentThread().wait();
            } catch (InterruptedException e) {
                
                e.printStackTrace();
            }finally {
                System.out.println("===========");
                
                this.notify();//Thread.currentThread().notifyAll();也不行
            }
        }
    }
}

}

AGB的主页 AGB | 初学一级 | 园豆:195
提问于:2019-01-23 10:12
< >
分享
最佳答案
0

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

}

收获园豆:10
天灯石 | 菜鸟二级 |园豆:212 | 2019-02-01 16:30
其他回答(1)
0

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

七月风 | 园豆:202 (菜鸟二级) | 2019-02-16 11:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册