[英]How to make two thread wait for each other outcome?
这是我的示例代码。
class A implements Runnable{
//stuff
Thread thr = new Thread(this);
boolean flag;
public void run()
{
while(true){
if(condition)flag = true;
}
}
}
class B implements Runnable{
//stuff
A a = new A();
Thread thr = new Thread(this);
public void run()
{
while(true){
//i ll do some thing here
if(a.flag == true)System.out.println("Kaboom");
}
}
public static void main(String[] args)
{
B b = new B();
}
}
所以事情是我在a之前开始b并且我希望b等到a.flag == true以激活“Kaboom”并且a.thr必须等待b在run()方法中执行它的工作。 我试过这个,但它不起作用
class A implements Runnable{
//stuff
Thread thr = new Thread(this);
boolean flag;
public void run()
{
while(true){
if(condition)flag = true;
synchronized(B.class){
this.flag=true;
B.class.notifyAll();
}
}
}
}
class B implements Runnable{
//stuff
A a = new A();
Thread thr = new Thread(this);
public void run()
{
while(true){
synchronized(this){
while(a.flag!=true)
{
this.wait();
}}
}
}
public static void main(String[] args)
{
B b = new B();
}}
我的同步块一定有问题,但我不知道是什么。
这可能是一个愚蠢的问题,但我只是JAVA的初学者,我真的没有得到那些线程的东西以及它是如何工作的。 Plz帮助我
我喜欢你使用wait / notifyAll的原始方法,使得线程不会使用CPU,直到满足条件才能恢复运行。 这是一种保持这种方法的解决方案。
几点说明:
1 - 在类对象上进行同步时要小心。 除非您真的想要同步整个类,否则请创建一个Object并将其用作锁。
2 - 使用volatile
关键字确保Java不创建变量的线程本地版本,并且对其值的更改会立即反映到其他线程。
public class Threads {
private final Object lock = new Object();
private volatile boolean flag;
class RunnableA implements Runnable {
private volatile boolean condition = false;
@Override
public void run() {
while (true) {
if (condition) {
if (!flag) {
synchronized (lock) {
System.out.println("Setting Flag to True");
flag = true;
lock.notifyAll();
}
}
} else {
System.out.println("Condition is False");
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
}
}
}
}
}
class RunnableB implements Runnable {
@Override
public void run() {
while (true) {
while (flag == false) {
synchronized (lock) {
if (flag == false) {
try {
lock.wait();
} catch (InterruptedException ex) {
}
}
}
}
System.out.println("Kaboom");
}
}
}
public void run() {
RunnableA runnableA = new RunnableA();
RunnableB runnableB = new RunnableB();
Thread t1 = new Thread(runnableA);
Thread t2 = new Thread(runnableB);
t1.start();
t2.start();
try {
Thread.sleep(5000L);
} catch (InterruptedException ex) {
}
runnableA.condition = true;
}
public static void main(String[] args) {
new Threads().run();
}
}
您创建了一个Runnable但没有在一个线程中启动它。
适当的一个:
import java.lang.*;
import java.util.concurrent.atomic.*;
class A implements Runnable {
AtomicBoolean flag;
AtomicBoolean condition;
A() {
flag = new AtomicBoolean();
condition = new AtomicBoolean();
}
public void run(){
while (true) {
System.out.println("A");
if (condition.get()) {
flag.set(true);
return ;
}
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class B implements Runnable {
A a;
B() {
a = new A();
}
public void run() {
while (true) {
System.out.println("B");
if (a.flag.get()) {
System.out.println("Kaboom");
return ;
}
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
B b = new B();
new Thread(b).start();
new Thread(b.a).start();
b.a.condition.set(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.