java多线程题目:子线程循环10次,主线程接着循环100次,如此循环50次

版权声明:转载请注明出处,谢谢。 https://blog.csdn.net/butterfly5211314/article/details/82670073

主要思路是将子线程循环10次,主线程接着循环100次这个作为一个整体考虑,
如此循环50次作为外部调用, 我们不要对外部调用作任何假设, 不管是50次还是100次。

因为子线程和主线程有一个“交替运行”的效果, 所以要作为一个整体,整体内部实现交替, 应该说是属于“组件内部的逻辑”。

首先实现这个子线程循环10次,主线程接着循环100次, 叫Business类:

class Business {
    private boolean shouldSubRun = true; // 子线程先执行

    public synchronized void sub(int seq) {
        while (!shouldSubRun) { // 还不该子线程执行, 就等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        for (int i = 1; i <= 10; i++) {
            System.out.println("sub-" + seq + "-" + i);
        }
        shouldSubRun = false; // 执行完修改标志
        this.notifyAll(); // 并唤醒别的线程
    }

    // 主线程的逻辑和子线程类似。
    public synchronized void main(int seq) {
        while (shouldSubRun) {
            try {
                this.wait();
            } catch (InterruptedException e) {
            }
        }

        for (int i = 1; i <= 100; i++) {
            System.out.println("main-" + seq + "-" + i);
        }
        shouldSubRun = true;
        this.notifyAll();
    }
}

sub方法为例子, synchronized和wait的对象要是同一个, 这里都是this
这个Business类就封装了子线程循环10次,主线程接着循环100次的“业务逻辑”。

那么如此循环50次就简单多了:

Business business = new Business();

// sub thread
new Thread(new Runnable() {

    @Override
    public void run() {
        for (int i = 1; i <= ROUND; i++) {
            business.sub(i);
        }
    }
}).start();

// main thread
new Thread(new Runnable() {

    @Override
    public void run() {
        for (int i = 1; i <= ROUND; i++) {
            business.main(i);
        }
    }
}).start();

这里起两个线程,分别执行循环50次的操作, 由于Business封装了业务逻辑, 所以外部不用管顺序的问题。

完整代码如下:

package cn.demo;

public class BusinessThreadTest {

    private static final int ROUND = 50;

    public static void main(String[] args) {

        Business business = new Business();

        // sub thread
        new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 1; i <= ROUND; i++) {
                    business.sub(i);
                }
            }
        }).start();

        // main thread
        new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 1; i <= ROUND; i++) {
                    business.main(i);
                }
            }
        }).start();
    }

}

class Business {
    private boolean shouldSubRun = true; // 子线程先执行

    public synchronized void sub(int seq) {
        while (!shouldSubRun) { // 还不该子线程执行, 就等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        for (int i = 1; i <= 10; i++) {
            System.out.println("sub-" + seq + "-" + i);
        }
        shouldSubRun = false; // 执行完修改标志
        this.notifyAll(); // 并唤醒别的线程
    }

    // 主线程的逻辑和子线程类似。
    public synchronized void main(int seq) {
        while (shouldSubRun) {
            try {
                this.wait();
            } catch (InterruptedException e) {
            }
        }

        for (int i = 1; i <= 100; i++) {
            System.out.println("main-" + seq + "-" + i);
        }
        shouldSubRun = true;
        this.notifyAll();
    }
}

参考:
http://blog.51cto.com/jiangzuun2014/1538205


欢迎补充指正!

猜你喜欢

转载自blog.csdn.net/butterfly5211314/article/details/82670073