gpt4 book ai didi

java - 线程方法未运行?

转载 作者:行者123 更新时间:2023-11-29 05:32:14 25 4
gpt4 key购买 nike

当我编译并运行 StartThreads 类时,我得到一个从 1 到 1000000 的整数列表,其中包含 false,最后打印出 true;现在我想知道的是,为什么 threadone 类在它应该打印一次 MyVariables 类中的实例变量 = true 时不打印任何内容? 公共(public)类 MyVariables { public boolean startApp = false;

public class ThreadOne implements Runnable {
Thread t;
MyVariables x;
public ThreadOne(MyVariables x) {
t = new Thread(this, "Thread One");
this.x = x;
}
@Override
public void run() {
while(this.x.startApp != false) {
System.out.println("Starting");
}
}
public void start() {
t.start();
}

}

public class ThreadTwo implements Runnable {
Thread t;
MyVariables x;
public ThreadTwo(MyVariables x) {
t = new Thread(this, "Thread One");
this.x = x;
}
@Override
public void run() {
synchronized(this.x) {
for(int i = 0; i <= 1000001; i++) {
if(i == 1000001) {
this.x.startApp = true;
System.out.println(this.x.startApp);

}
else {
System.out.println(this.x.startApp);
System.out.println(i);
}
}
}

}
public void start() {
t.start();
}

}


public class StartThreads {

public static void main(String[] args) {
MyVariables a = new MyVariables();
ThreadOne x = new ThreadOne(a);
ThreadTwo y = new ThreadTwo(a);
x.start();
y.start();
}

}

最佳答案

为什么 ThreadOne 会像您所说的那样表现? while 循环永远不会运行,而是会被跳过,因为条件不成立。我认为您希望它等待某项变为真,但它什么也没做,而是一旦发现条件为假,就结束执行。

请注意,这是丑陋的代码:

while(this.x.startApp != false) {

当某事不是假的时为什么要声明?这等同于真实。更好

while (x.startApp) {

现在至于您的实际问题,也许您应该改用 while 循环:

while (!x.startApp) {
Thread.sleep(1); // surrounded with try/catch
}
System.out.println("Starting");

例如,

class ThreadOne implements Runnable {
Thread t;
volatile MyVariables x;

public ThreadOne(MyVariables x) {
t = new Thread(this, "Thread One");
this.x = x;
}

@Override
public void run() {
while (!x.startApp) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
System.out.println("Starting");
}

public void start() {
t.start();
}

}

class ThreadTwo implements Runnable {
private static final int MAX_I = 10001;
Thread t;
volatile MyVariables x;

public ThreadTwo(MyVariables x) {
t = new Thread(this, "Thread One");
this.x = x;
}

@Override
public void run() {
synchronized (this.x) {
for (int i = 0; i <= MAX_I; i++) {
if (i == MAX_I) {
this.x.startApp = true;
System.out.println(this.x.startApp);

} else {
System.out.println(this.x.startApp);
System.out.println(i);
}
}
}

}

public void start() {
t.start();
}

}

public class StartThreads {

public static void main(String[] args) {
MyVariables a = new MyVariables();
ThreadOne x = new ThreadOne(a);
ThreadTwo y = new ThreadTwo(a);
x.start();
y.start();
}

}

class MyVariables {
public volatile boolean startApp = false;
}

此外,我认为您的 boolean 字段至少应该是可变的。


另一种使用 PropertyChangeListener 和观察者模式的方法:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class StartThreads2 {
public static void main(String[] args) {
final MyVariables2 myVars2 = new MyVariables2();
final RunOne runOne = new RunOne();
final RunTwo runTwo = new RunTwo(myVars2);

myVars2.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (MyVariables2.START_APP.equals(pcEvt.getPropertyName())) {
if (pcEvt.getNewValue().equals(Boolean.TRUE)) {
new Thread(runOne).start();
}
}
}
});

new Thread(runTwo).start();
}
}

class MyVariables2 {
public static final String START_APP = "start app";
private volatile boolean startApp = false;
private PropertyChangeSupport pcSupport = new PropertyChangeSupport(this);

public boolean isStartApp() {
return startApp;
}

public void setStartApp(boolean startApp) {
boolean oldValue = this.startApp;
boolean newValue = startApp;
this.startApp = startApp;
pcSupport.firePropertyChange(START_APP, oldValue, newValue);
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
}

class RunOne implements Runnable {
@Override
public void run() {
System.out.println("Starting RunOne");
}
}

class RunTwo implements Runnable {
private static final int MAX_I = 10001;
private MyVariables2 myVars2;

public RunTwo(MyVariables2 myVars2) {
this.myVars2 = myVars2;
}

@Override
public void run() {
for (int i = 0; i <= MAX_I; i++) {
System.out.println("startApp: " + myVars2.isStartApp());
System.out.printf("i: %05d%n", i);
}

myVars2.setStartApp(true);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
System.out.println("startApp: " + myVars2.isStartApp());
}
}

关于java - 线程方法未运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20718709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com