Swing 中两个线程上的同步块不起作用
Synchronized block on two threads in Swing not working
在概述问题之前,让我向您描述一下我的问题的背景。我目前正在编写一个游戏引擎关卡编辑器,我正在开发 class,它将充当用户与之交互以构建关卡的屏幕。我想让屏幕与编辑器的大小成比例。
当我开始调整屏幕大小并同时在屏幕上绘图时,就会出现问题。我从一个线程绘制,同时我从另一个线程(EDT)编辑我正在绘制的原始像素阵列的大小。我知道这是一个很大的禁忌,所以很自然,没有安全措施,我在调整大小时偶尔会遇到 IndexOutOfBounds 异常。
我的想法是,我可以在调整大小代码和绘图代码上添加一个同步块。这样,就不会有冲突,应该避免这个问题。但是,同步被完全忽略了。我仍然遇到同样的错误,我真的很困惑为什么它不起作用。以下是两种感兴趣的方法:
public void setPixel(int r, int g, int b, int x, int y) {
synchronized (pixels){
System.out.println("Start Draw...");
int color = (r << 16) | (g << 8) | b;
pixels[y * screenWidth + x] = color;
System.out.println("End Draw...");
}
}
@Override
public void componentResized(ComponentEvent e) {
synchronized (pixels) {
System.out.println("Start resize");
int width = e.getComponent().getWidth();
int height = e.getComponent().getHeight();
float aspectRatio = 4 / 3f;
if (width > height) {
width = (int) (height * aspectRatio);
} else if (height > width) {
height = width;
}
if (width < 0 || height < 0) {
width = 1;
height = 1;
}
this.screenWidth = width;
this.screenHeight = height;
image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
System.out.println("End Resize");
}
}
我不知道这是否重要(应该不对吧?)但我的屏幕 class 扩展了 AWT Canvas。它还是其父组件的侦听器,因此当它调整大小时,它会触发一个事件,触发 componentResized 被调用。无论如何,谢谢您,我们将不胜感激。
编辑:我的绘图代码可以在下面找到。
new Thread(new Runnable(){
@Override
public void run() {
while(true) {
for (int y = 0; y < screen.getHeight(); y++) {
for(int x = 0; x < screen.getWidth(); x++){
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
screen.setPixel(r, g, b, x, y);
}
}
}
}
}).start();
我还有别的想法。不仅是问题评论里的内容。
让我先修改setPixel
方法:
public void setPixel(int r, int g, int b, int x, int y) {
System.out.println("Called with: " + x + ", " + y); //Just added a print statement.
synchronized (mySyncGuard){
System.out.println("Start Draw...");
int color = (r << 16) | (g << 8) | b;
pixels[y * screenWidth + x] = color;
System.out.println("End Draw...");
}
}
其中 mySyncGuard 是最终的 属性,用作 setPixel
和 componentResized
的同步保护。
现在想象一下以下场景:
有一个调用setPixel
方法的循环:这个循环调用从x = 0
和y = 0
到x < screenWidth
和y < screenHeight
的方法!喜欢下面的:
for (int x = 0; x < screenWidth; ++x)
for (int y = 0; y < screenHeight; ++y) {
int r = calculateNextR(),
g = calculateNextG(),
b = calculateNextB();
setPixel(r, g, b, x, y);
}
其中 calculateNextR()、calculateNextG() 和 calculateNextB() 是分别生成下一个红色、绿色和蓝色分量的方法。
现在,例如,让 screenWidth 为 200,screenHeight 也为 200,并在某些时候调整为 100x100。
现在的问题是当组件即将调整为 100、100 时,x 和 y 分别为 150 和 150。
我们的自定义 setPixel
方法现在使用 x==150 和 y==150 调用。 但是 在我们添加的显示 x 和 y 值的打印语句之前,componentResized
设法被调用并获取了同步守卫 属性 我的同步卫士!所以 componentResized
现在正在做它的工作,将图像的大小更改为 100x100,这反过来又将 pixels 数组更改为 smaller[=75] =] 比之前的数据数组。
同时,setPixel
现在打印"Called with 150, 150"并在同步块等待获得mySyncGuard[=75的锁=],因为 componentResized
目前已经获得它并相应地将图像更改为 100x100 和 像素 数组。
所以现在pixels数组变小了,componentResized
完成调整大小,最后setPixel可以获得mySyncGuard的锁。
现在的问题是:数组像素在位置 150,150 处遍历,而实际大小为 100x100。所以你去吧! IndexOutOfBoundsException...
结论:
- 我们需要您提供更多代码来确定问题所在。
- 变化的变量(如screenWidth、screenHeight、image和pixels) 需要在所有地方同步,不仅在
setPixel
和 componentResized
方法内部。例如,如果你的情况就像我刚才描述的场景,那么不幸的是 for 循环也应该在 synchronized (mySyncGuard)
块中。
- 这不适合发表评论。如果您 post 更多代码,那么我们可能会告诉您出了什么问题(如果不需要,我可能会删除此答案)。
在概述问题之前,让我向您描述一下我的问题的背景。我目前正在编写一个游戏引擎关卡编辑器,我正在开发 class,它将充当用户与之交互以构建关卡的屏幕。我想让屏幕与编辑器的大小成比例。
当我开始调整屏幕大小并同时在屏幕上绘图时,就会出现问题。我从一个线程绘制,同时我从另一个线程(EDT)编辑我正在绘制的原始像素阵列的大小。我知道这是一个很大的禁忌,所以很自然,没有安全措施,我在调整大小时偶尔会遇到 IndexOutOfBounds 异常。
我的想法是,我可以在调整大小代码和绘图代码上添加一个同步块。这样,就不会有冲突,应该避免这个问题。但是,同步被完全忽略了。我仍然遇到同样的错误,我真的很困惑为什么它不起作用。以下是两种感兴趣的方法:
public void setPixel(int r, int g, int b, int x, int y) {
synchronized (pixels){
System.out.println("Start Draw...");
int color = (r << 16) | (g << 8) | b;
pixels[y * screenWidth + x] = color;
System.out.println("End Draw...");
}
}
@Override
public void componentResized(ComponentEvent e) {
synchronized (pixels) {
System.out.println("Start resize");
int width = e.getComponent().getWidth();
int height = e.getComponent().getHeight();
float aspectRatio = 4 / 3f;
if (width > height) {
width = (int) (height * aspectRatio);
} else if (height > width) {
height = width;
}
if (width < 0 || height < 0) {
width = 1;
height = 1;
}
this.screenWidth = width;
this.screenHeight = height;
image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
System.out.println("End Resize");
}
}
我不知道这是否重要(应该不对吧?)但我的屏幕 class 扩展了 AWT Canvas。它还是其父组件的侦听器,因此当它调整大小时,它会触发一个事件,触发 componentResized 被调用。无论如何,谢谢您,我们将不胜感激。
编辑:我的绘图代码可以在下面找到。
new Thread(new Runnable(){
@Override
public void run() {
while(true) {
for (int y = 0; y < screen.getHeight(); y++) {
for(int x = 0; x < screen.getWidth(); x++){
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
screen.setPixel(r, g, b, x, y);
}
}
}
}
}).start();
我还有别的想法。不仅是问题评论里的内容。
让我先修改setPixel
方法:
public void setPixel(int r, int g, int b, int x, int y) {
System.out.println("Called with: " + x + ", " + y); //Just added a print statement.
synchronized (mySyncGuard){
System.out.println("Start Draw...");
int color = (r << 16) | (g << 8) | b;
pixels[y * screenWidth + x] = color;
System.out.println("End Draw...");
}
}
其中 mySyncGuard 是最终的 属性,用作 setPixel
和 componentResized
的同步保护。
现在想象一下以下场景:
有一个调用setPixel
方法的循环:这个循环调用从x = 0
和y = 0
到x < screenWidth
和y < screenHeight
的方法!喜欢下面的:
for (int x = 0; x < screenWidth; ++x)
for (int y = 0; y < screenHeight; ++y) {
int r = calculateNextR(),
g = calculateNextG(),
b = calculateNextB();
setPixel(r, g, b, x, y);
}
其中 calculateNextR()、calculateNextG() 和 calculateNextB() 是分别生成下一个红色、绿色和蓝色分量的方法。
现在,例如,让 screenWidth 为 200,screenHeight 也为 200,并在某些时候调整为 100x100。
现在的问题是当组件即将调整为 100、100 时,x 和 y 分别为 150 和 150。
我们的自定义 setPixel
方法现在使用 x==150 和 y==150 调用。 但是 在我们添加的显示 x 和 y 值的打印语句之前,componentResized
设法被调用并获取了同步守卫 属性 我的同步卫士!所以 componentResized
现在正在做它的工作,将图像的大小更改为 100x100,这反过来又将 pixels 数组更改为 smaller[=75] =] 比之前的数据数组。
同时,setPixel
现在打印"Called with 150, 150"并在同步块等待获得mySyncGuard[=75的锁=],因为 componentResized
目前已经获得它并相应地将图像更改为 100x100 和 像素 数组。
所以现在pixels数组变小了,componentResized
完成调整大小,最后setPixel可以获得mySyncGuard的锁。
现在的问题是:数组像素在位置 150,150 处遍历,而实际大小为 100x100。所以你去吧! IndexOutOfBoundsException...
结论:
- 我们需要您提供更多代码来确定问题所在。
- 变化的变量(如screenWidth、screenHeight、image和pixels) 需要在所有地方同步,不仅在
setPixel
和componentResized
方法内部。例如,如果你的情况就像我刚才描述的场景,那么不幸的是 for 循环也应该在synchronized (mySyncGuard)
块中。 - 这不适合发表评论。如果您 post 更多代码,那么我们可能会告诉您出了什么问题(如果不需要,我可能会删除此答案)。