我在一个线程中有以下方法,它可以 100% 工作,但是一旦我删除 System.out.println("here");
代码就会停止执行它正在执行的操作。没有任何错误,只是看起来什么也没做。它的作用是使图像的颜色变亮,其中包含调试线会变亮,没有调试线则不会变亮。为什么会造成这种情况?
线程类:
package pocketshop.threads;
import com.jogamp.opencl.CLBuffer;
import java.awt.Container;
import java.awt.image.BufferedImage;
import java.nio.FloatBuffer;
import pocketshop.Canvas;
import pocketshop.graphics.CL;
import pocketshop.graphics.Preview;
/**
*
* @author Ryan
*/
public class AdjustThread extends Thread {
protected float amount = 0;
protected CLBuffer<FloatBuffer> buffer;
protected String adjustment;
protected Container parent;
public AdjustThread(Container parent, String adjustment) {
this.parent = parent;
this.adjustment = adjustment;
}
public void setAmount(float amount){
this.amount = amount;
}
public CLBuffer<FloatBuffer> getBuffer() {
return buffer;
}
public void run() {
float cAmount = 0;
while(true){
System.out.println("here");
if(cAmount != this.amount){
cAmount = this.amount;
CL.start(adjustment, this.amount);
buffer = CL.getBuffer();
float[] pixels = CL.getPixels();
BufferedImage newimage = new BufferedImage(Canvas.image.getWidth(), Canvas.image.getHeight(), BufferedImage.TYPE_INT_RGB);
buffer.getBuffer().get(pixels).rewind();
newimage.getRaster().setPixels(0, 0, Canvas.image.getWidth(), Canvas.image.getHeight(), pixels);
Preview.setImage(newimage);
Canvas.preview = Preview.getImage();
parent.repaint();
}
}
}
}
以及对话框相关代码:
package pocketshop.dialogs;
import java.awt.image.BufferedImage;
import pocketshop.Canvas;
import pocketshop.graphics.adjustments.Contrast;
import pocketshop.threads.AdjustThread;
/**
*
* @author Ryan
*/
public class BrightnessContrastDialog extends javax.swing.JDialog {
AdjustThread adj;
/**
* Creates new form BrightnessContrastDialog
*/
public BrightnessContrastDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
adj = new AdjustThread(this.getParent(), "Brightness");
adj.start();
}
// Run everytime the JSlider moves
private void sldBrightnessStateChanged(javax.swing.event.ChangeEvent evt) {
float val = sldBrightness.getValue();
txtBrightness.setText("" + (int) val);
adj.setAmount(val);
}
}
最佳答案
this.amount
是否声明为 volatile
?
我认为该字段由于另一个线程中的分配而发生变化。如果它没有声明为 volatile
,则没有理由假设运行上面的run()
方法的线程会观察到它的变化。第一次将 this.amount
分配给 cAmount
后,无论如何,从该线程的角度来看,它们此后保持相等。
一旦您向我们澄清 this.amount
上声明的限定符并显示在其他地方更改的代码片段,我们就可以帮助指定恢复您想要的行为所需的正确同步设备。
至于为什么调用 PrintStream#println()
似乎在这里有所不同,它可能不仅会导致延迟,而且还可能会遇到 happens-before 内存可见性边缘,允许对此线程可见的 this.amount
更改。这是很多挥手的举动,但我认为在确定该特定副作用的根本原因之前,这里还有更大的问题需要首先解决。
关于java - 循环不执行内部if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436016/