美文网首页
多线程练习:主/子线程交替循环

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

作者: 一个搬砖小能手 | 来源:发表于2018-10-27 17:49 被阅读7次

题目:

编写程序实现,子线程循环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");
    }
}

相关文章

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

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

  • 多线程交替打印1~10的奇偶数

    题外话 今天终于周末了,没有出去运动,最近学了多线程,想练习下,找了到练习题,多线程交替打印1到100的奇偶数。 ...

  • Python程序员都知道的入门知识の九

    目录【Python程序员都知道的入门知识】 1. 多线程练习 for循环启动五条线程,打印当前处于活跃状态的线程数...

  • 线程间通信实例

    如何让两个线程依次执行? 主线程等待子线程运行 那如何让 两个线程按照指定方式有序交叉运行呢? 多线程交替打印AB...

  • iOS - 多线程NSThread的使用

    一、多线程的基本概念: 1. 多线程的原理: 多线程是循环切换执行的 线程执行完毕之后会自动销毁 2. 多线程的优...

  • 多线程交替打印

    多线程打印1~100: 但是我没想明白count为什么不加voliatle也可以。

  • 多线程基本概念

    一、什么进程 二、什么线程 三、什么是多线程 四、主线程和子线程 主线程: 子线程: 多线程的优缺点优点: 缺点 ...

  • Python-学习之路-15 多线程1

    多线程 全局解释器(GIL) Python 代码的执行由Python虚拟机进行控制 在主循环中只能有一个控制线程在...

  • iOS线程安全问题

    多线程 线程与队列区别 程序分为主线程与子线程, 主线程主要用来更新队列,而主线程所有要处理的事务都放在主队列,主...

  • 2020-07-02【多线程】

    进程 线程 多线程的实现方式1 设置/获取线程名称 线程调度 线程控制 线程生命周期 多线程实现方式2 练习 同步...

网友评论

      本文标题:多线程练习:主/子线程交替循环

      本文链接:https://www.haomeiwen.com/subject/jcidtqtx.html