在java中同步两个线程 [英] synchronize two threads in java

查看:48
本文介绍了在java中同步两个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java程序中有两个线程,一个是主线程,另一个线程是在主线程中产生的线程A.现在我希望主线程启动线程 A 并等待线程 A 在 run 方法中执行了其部分代码,并且线程 A 应该挂起自己.然后主线程应该开始运行,运行几行代码,然后线程 A 应该从它停止的地方开始,反之亦然.这应该发生 n 次.我正在尝试如下:

I have two threads in my java programme, one is main thread and other thread is thread A which is spawned in main thread. now i want main thread to start thread A and wait till thread A has executed some part of its code in run method and thread A should suspend itself. main thread should then start running, run few lines of code and then again thread A should start from where it has stopped and vice versa. this should happen for n number of times. I am trying as belows:

线程A类:

public class ThreadA implements Runnable {
    boolean suspended = false;
    boolean stopped = false;
    synchronized void stop() {
        stopped = true;
        suspended = false;
        notify();
    }
    synchronized void suspend() {
        suspended = true;
    }
    synchronized void resume() {
        suspended = false;
        notify();
    }
    void job() throws InterruptedException {
        for (int i = 0; i < 5; i++)
            synchronized (this) {
                System.out.println("performing job.");
                suspend();
                while (suspended) {
                    notify();
                    suspended = false;
                }
            }
    }
    @Override
    public void run() {
        try {
            job();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

主线程:

public class MainThread {
    public static void main(String[] args) throws InterruptedException {
        ThreadA a1=new ThreadA();
        Thread t1=new Thread(a1);
        synchronized (t1) {
            t1.start();
            for (int i = 0; i < 5; i++) {
                t1.wait();
                System.out.println("perform some action");
                a1.resume();
            }

        }
    }
}

预期输出:

performing job.
perform some action
performing job.
perform some action
performing job.
perform some action
performing job.
perform some action
performing job.
perform some action

实际输出:

performing job.
performing job.
performing job.
performing job.
performing job.
perform some action

即使我在作业方法中发出了 notify() 信号,我也不知道为什么整个 for 循环会在线程 A 中执行.

I don't know why the whole for loop is getting executed in Thread A even when i've issued a notify() signal in job method.

推荐答案

这里有更简单的方法

public class TwoThread {

    public static void main(String[] args) throws InterruptedException {
        ThreadA a1 = new ThreadA();
        Thread t1 = new Thread(a1);

        synchronized (a1) {
            t1.start();
            for (int i = 0; i < 5; i++) {
                a1.wait();
                System.out.println("perform some action " + i);
                a1.notify();
            }

        }
    }
}

public class ThreadA implements Runnable {
    boolean suspended = false;
    boolean stopped = false;

    void job() throws InterruptedException {
        for (int i = 0; i < 5; i++)
            synchronized (this) {
                System.out.println("performing job. " + i);
                notify();
                wait();
            }
    }

    public void run() {
        try {
            job();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

这篇关于在java中同步两个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆