在java中恢复线程

这就是我想要完成的!

步骤1)启动th1并转到其runnable

步骤2)在th1的runnable内启动th2

步骤3)在runnable的中间,加入th2并转到th2的runnable

步骤4)在th2的runnable中间,加入th1并返回到我在th1 runnable中剩下的位置

步骤5)在完成run1之后,返回th2 runnable到达我离开的地方并完成th2 runnable

步骤6)程序结束

第4步和第5步是我的问题 . 我无法正确执行它们 .

public class Threads2 {
    class alphabet1 implements Runnable {

        @Override
        public void run() {

            alphabet2 alpha2= new alphabet2();
            Thread th2 = new Thread(alpha2);
            System.out.println("A");
            System.out.println("B");
            System.out.println("C");
            System.out.println("D");

            th2.start();
            try {
                th2.join();
            }catch (InterruptedException e) {
            }catch (IllegalMonitorStateException e1){
                System.out.println("Thread interrupted!");
            }

            System.out.println("G");
            System.out.println("H");
        }
    }
    class alphabet2 implements Runnable {

        @Override
        public void run() {

            alphabet alpha= new alphabet();
            Thread th1 = new Thread(alpha);
            System.out.println("E");
            System.out.println("F");

            try {
                th1.join();
            }catch (InterruptedException e){
            }catch (IllegalMonitorStateException e1){
                System.out.println("Thread interrupted!");
            }

            System.out.println("I");

        }
    }

    public static void main(String[] args){
        Threads2 obj = new Threads2();

        alphabet1 alpha  = obj.new alphabet1(); 

        Thread th1 = new Thread(alpha);
        th1.start();

    }
}

输出:A B C D E F I G H.

“我”应该是输出中的最后一个 . 我知道为什么它以不正确的顺序显示,但是,我无法弄清楚如何以正确的顺序显示它?我一起使用了notify()和wait() . 如果我在“alphabet2”中键入“th1.start()”,则在“alphabet”类之后将重新打印“A” . 我也试过“interrupt()”和“sleep()” . 我理解我的代码有缺陷,我只是以代码为例 .

回答(1)

2 years ago

这是一个基于您的代码的可能解决方案 .

public class Main {

    private static final Object LOCK = new Object();

    public static void main(String[] args) {
        alphabet1 alpha = new alphabet1();
        new Thread(alpha).start();
    }

    static class alphabet1 implements Runnable {

        @Override
        public void run() {

            System.out.println("A");
            System.out.println("B");
            System.out.println("C");
            System.out.println("D");

            try {
                synchronized (LOCK) {
                    alphabet2 alpha2 = new alphabet2();
                    new Thread(alpha2).start();
                    LOCK.wait();
                }
            } catch (InterruptedException | IllegalMonitorStateException e) {
                e.printStackTrace();
            }

            System.out.println("G");
            System.out.println("H");

            try {
                synchronized (LOCK) {
                    LOCK.notifyAll();
                }
            } catch (IllegalMonitorStateException e) {
                e.printStackTrace();
            }
        }
    }

    static class alphabet2 implements Runnable {

        @Override
        public void run() {

            System.out.println("E");
            System.out.println("F");

            try {
                synchronized (LOCK) {
                    LOCK.notifyAll();
                    LOCK.wait();
                }
            } catch (InterruptedException | IllegalMonitorStateException e) {
                e.printStackTrace();
            }

            System.out.println("I");
        }
    }
}