线程的交互执行(面试题)

要求:子线程运行执行10次后,主线程再运行5次,这样交替运行三遍

public class ThreadTest {

    public static void main(String[] args) {
        Business business = new Business();
        //子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<3;i++){
                    business.subMethod();
                }
            }
        }).start();

        //主线程
        for(int i=0;i<3;i++){
            business.mainMethod();
        }
    }

    static class Business{
        private boolean subFlag = true; //标识子进程是否可以运行

        public synchronized void mainMethod(){
            while(subFlag){ //当子进程运行时,主进程等待
                try{
                    wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }

            //当subFlag=false时,主进程可以运行
            for(int i=0;i<5;i++){ //主进程运行5次
                System.out.println(Thread.currentThread().getName()+
                        ":main method running "+i);
            }

            //主进程运行完之后将subFlag=true,并唤醒等待的子进程
            subFlag=true;
            notify();
        }

        public synchronized void subMethod(){
            while(!subFlag){ //当subFlag = false时,子进程需等待
                try{
                    wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }

            //当subFlag = true时,运行十次子进程
            for(int i=0;i<10;i++){
                System.out.println(Thread.currentThread().getName()+
                        ":sub method running "+i);
            }

            //子进程运行结束,subFlag = false,唤醒父进程
            subFlag = false;
            notify();
        }

    }
}

运行结果:

Thread-0:sub method running 0
Thread-0:sub method running 1
Thread-0:sub method running 2
Thread-0:sub method running 3
Thread-0:sub method running 4
Thread-0:sub method running 5
Thread-0:sub method running 6
Thread-0:sub method running 7
Thread-0:sub method running 8
Thread-0:sub method running 9
main:main method running 0
main:main method running 1
main:main method running 2
main:main method running 3
main:main method running 4
Thread-0:sub method running 0
Thread-0:sub method running 1
Thread-0:sub method running 2
Thread-0:sub method running 3
Thread-0:sub method running 4
Thread-0:sub method running 5
Thread-0:sub method running 6
Thread-0:sub method running 7
Thread-0:sub method running 8
Thread-0:sub method running 9
main:main method running 0
main:main method running 1
main:main method running 2
main:main method running 3
main:main method running 4
Thread-0:sub method running 0
Thread-0:sub method running 1
Thread-0:sub method running 2
Thread-0:sub method running 3
Thread-0:sub method running 4
Thread-0:sub method running 5
Thread-0:sub method running 6
Thread-0:sub method running 7
Thread-0:sub method running 8
Thread-0:sub method running 9
main:main method running 0
main:main method running 1
main:main method running 2
main:main method running 3
main:main method running 4

你可能感兴趣的:(java基础)