I have two threads. One is EDT (using the swingUtilities.invokeLater) and the other is for calculation. I get the input from a mouse click handled in the EDT and then perform the calculation in the other. But I get the following output (the black numbers are in EDT, the red are in the other):
我有两个主题。一个是EDT(使用swingUtilities.invokeLater),另一个是计算。我通过在EDT中处理的鼠标单击获得输入,然后在另一个中执行计算。但我得到以下输出(黑色数字在EDT中,红色在另一个中):
So it seems that the calculation occurs before I read the data from GUI. Could anyone explain why?
因此,似乎在我从GUI读取数据之前进行了计算。谁有人解释为什么?
Edit: Some code:
编辑:一些代码:
//in EDT
public void mousePressed (MouseEvent e)
{
//get the position
System.out.println(i + " " + j);---> black numbers
played = true;
}
//in thread 2
public boolean valid (Move m)
{
System.err.println(m.locX +" "+m.locY); ---> red numbers
//use the position get in EDT
System.out.println("finished");---> black 'finished'
}
3 个解决方案
#1
3
System.out and System.err are not "sequential" while printing to the output.
打印到输出时,System.out和System.err不是“顺序”的。
This simple code shows it:
这个简单的代码显示了它:
for (int i = 0; i < 100; i++)
{
System.out.println(i);
System.err.println(i);
}
You'd be expecting the out
to be executed before the err
, right? It is, the problem is, it is just not PRINTED in the same order.
你会期待在错误之前执行出局,对吧?问题是,它只是以相同的顺序打印。
#2
1
System.out.println()
can be assigned a lower priority than other types of output, and get printed after them, even if they are called before. It depends on what you are running your program on.
可以为System.out.println()分配比其他类型的输出更低的优先级,并在它们之后打印,即使它们之前被调用过。这取决于您运行程序的内容。
For example, in my setup, if I print something then throw an exception, the exception can come before the printed content.
例如,在我的设置中,如果我打印一些内容然后抛出异常,则异常可以在打印内容之前出现。
#3
0
You have not given any indication as to whether your operation is thread safe. It seems that your red thread has the ability to attempt processing on black thread's output, and ends up reporting finished simply because it doesn't account for the black thread never having ran ( at least for this iteration )
您没有说明您的操作是否是线程安全的。看来你的红色线程有能力尝试处理黑线程的输出,并最终报告完成只是因为它没有考虑到从未运行的黑线程(至少对于这个迭代)
Your code should have something that accounts for this case, or you set up a producer consumer model, or find yet another way to arbitrate events between the two threads.
您的代码应具有解决此案例的内容,或者您设置生产者消费者模型,或者找到另一种方法来仲裁两个线程之间的事件。
#1
3
System.out and System.err are not "sequential" while printing to the output.
打印到输出时,System.out和System.err不是“顺序”的。
This simple code shows it:
这个简单的代码显示了它:
for (int i = 0; i < 100; i++)
{
System.out.println(i);
System.err.println(i);
}
You'd be expecting the out
to be executed before the err
, right? It is, the problem is, it is just not PRINTED in the same order.
你会期待在错误之前执行出局,对吧?问题是,它只是以相同的顺序打印。
#2
1
System.out.println()
can be assigned a lower priority than other types of output, and get printed after them, even if they are called before. It depends on what you are running your program on.
可以为System.out.println()分配比其他类型的输出更低的优先级,并在它们之后打印,即使它们之前被调用过。这取决于您运行程序的内容。
For example, in my setup, if I print something then throw an exception, the exception can come before the printed content.
例如,在我的设置中,如果我打印一些内容然后抛出异常,则异常可以在打印内容之前出现。
#3
0
You have not given any indication as to whether your operation is thread safe. It seems that your red thread has the ability to attempt processing on black thread's output, and ends up reporting finished simply because it doesn't account for the black thread never having ran ( at least for this iteration )
您没有说明您的操作是否是线程安全的。看来你的红色线程有能力尝试处理黑线程的输出,并最终报告完成只是因为它没有考虑到从未运行的黑线程(至少对于这个迭代)
Your code should have something that accounts for this case, or you set up a producer consumer model, or find yet another way to arbitrate events between the two threads.
您的代码应具有解决此案例的内容,或者您设置生产者消费者模型,或者找到另一种方法来仲裁两个线程之间的事件。