在java中同步两个线程

编程入门 行业动态 更新时间:2024-10-28 18:28:25
本文介绍了在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中同步两个线程

本文发布于:2023-10-26 22:18:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   两个   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!