多线程练习:主/子线程交替循环

题目:

编写程序实现,子线程循环10次,接着主线程循环20次,接着再子线程循环10次,主线程循环20次,如此反复,循环50次.

思路

  • 首先,既然是要求是交替进行。那么我们可以向wati/notify方法考虑。
  • 由于notify方法随机唤起一个,notifyAll唤起全部。那么我们势必需要一个标志位,标记哪个线程才应该执行。这里我们将这个标志位取名为runInSubThread。每个线程都通过判断runInSubThread来决定是进行10/20循环,还是直接wait
  • 主/子线程不能运行完就结束了,因此在各自的方法中应该使用while(true)保持线程不会一次就结束。
public class MainAndSub {
    public static boolean runInSubThread = true;
    public static boolean hasMainThreadFinished = false;
    public static boolean hasSubThreadFinished = false;

    //主线程中的计数器
    private int counterInMainThread = 0;

    public static class SubThread extends Thread{
        //子线程循环次数计数器
        private int counterInSubThread = 0;
        @Override
        public void run() {
            while (true) {
                synchronized (MainAndSub.class) {
                    if (counterInSubThread  > 50) {
                        hasSubThreadFinished = true;
                        System.out.println("Sub Thread finished");
                        break;
                    }
                    try {
                        if (!runInSubThread) {
                            wait();
                        }
                        for (int i = 0; i < 10; i++) {
                            //假装在运算
                            int x = i*i*i*i;
                        }
                        counterInSubThread += 1;
                        System.out.println("Sub Thread count == " + counterInSubThread);
                        /**
                         * 如果主线程还没有结束就唤醒主线程,然后自身等待;否则累加至最大值
                         */
                        if (!hasMainThreadFinished) {
                            runInSubThread = false;
                            wait();
                            notifyAll();
                        }
                    } catch (Exception e){
                        //e.printStackTrace();
                    }
                }
            }
        }
    }
    
   
    /*主线程中执行循环的方法在这里*/
    public void wordWithSubThread(){
        while (true) {
            synchronized (MainAndSub.class) {
                if (counterInMainThread >= 50){
                    hasMainThreadFinished = true;
                    System.out.println("wordWithSubThread Func finished");
                    break;
                }
                try {
                    if (runInSubThread) {
                        this.wait();
                    }
                    for (int i = 0; i < 20; i++) {
                        //假装在运算
                        int x = i*i*i*i;
                    }
                    counterInMainThread += 1;
                    System.out.println("Main Thread count == " + counterInMainThread);
                    if(!hasSubThreadFinished) {
                        runInSubThread = true;
                        this.wait();
                        this.notifyAll();
                    }

                } catch (Exception e){
                    //e.printStackTrace();
                }
            }
        }

    }
    public static void main(String[] args) throws InterruptedException {
        SubThread st = new SubThread();
        st.start();

        MainAndSub ms = new MainAndSub();
        ms.wordWithSubThread();
        System.out.println("Main Func finished");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 进程和线程 进程 所有运行中的任务通常对应一个进程,当一个程序进入内存运行时,即变成一个进程.进程是处于运行过程中...
    胜浩_ae28阅读 10,532评论 0 23
  • 线程是程序执行的最小单元,多线程是指程序同一时间可以有多个执行单元运行(这个与你的CPU核心有关)。 在java中...
    程序员技术圈阅读 4,514评论 2 17
  • 此片文章主要总结的是Thread类及相关的基础概念和API,首先需要厘清线程调度中的几个基本概念: 一、线程调度的...
    千淘萬漉阅读 7,410评论 0 2
  • 那天 我围着屋子找暖气片 没有暖气冬天可怎么过 后来想起来 这是上海 南方没有暖气 我是北方人
    公园阅读 1,794评论 1 3
  • 网络上有句话是这么说的,真正靠谱的人,凡事有交代,事事有着落,件件有着落。想送你回家的人,东南西北都顺路。愿陪你吃...
    我生为人阅读 5,430评论 0 1