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

题目:

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

你可能感兴趣的:(多线程练习:主/子线程交替循环)