Inexplicable Java fraud - perhaps with threads?

Problem

I'm currently halfway through creating the Game of Life simulator in Java (at Eclipse) using the Swing GUI as part of a college project. This happens surprisingly, except for one small flaw -

It works on my netbook, but not on any other PC that I have tried. This is under Ubuntu.

Some structural diagrams - I have a model, view and controller. I have not defined a model yet, but I made a view (part of the GUI) and started the controller. The controller is started by the Main method, and the controller then creates the View class in a separate thread and enters a while loop.

View implements the queue of "orders" that it received from user input, mouse clicks and what not. The controller selects these orders from the queue at the iteration of the while loop and executes them as necessary.

However, although the code works fine on my netbook (latest version, Java 1.6.0_20), it doesn't work on my PC (latest version again, Java 1.6.0_20) or college computers (karmic, some previous versions of Java). It just stops when it gets into the getNextCommand method. No errors, he just refuses to print / execute

The source files are here - http://www.mediafire.com/?dfwtdkj1tdxd5xl The files of interest are Controller and View.

Example

In the view, I have this function:

public Command getNextCommand() {
  System.out.println(commands.getFirst().id);
  return commands.pop(); 
 }

, getNextCommand(), , .

while :

while(!stop) {
   if (gui.hasCommand()){
    order = gui.getNextCommand();
    //System.out.println("Something");
    //if(order.id.equals("stop")) { stop = true; }
   }
  }

. getNextCommand, .

, . !

? , ?:

, .class, Eclipse, ( , ). , javac, .

!

,

.

, ( id (String), x, y (int) value (int)), getNextCommand, Integer. - .

+3
3

Uhm, , view controller . , .

, , , , . , - , , - synchronized.

: http://gee.cs.oswego.edu/dl/cpj/jmm.html

: java.util.vector LinkedList

+3

. commands?

, ( while), - /. BlockingQueue (, ArrayBlockingQueue LinkedBlockingQueue). , (), , .

.

BlockingQueue<Command> commands = new LinkedBlockingQueue<Command>();

//...

//wait for the next element and then get it
while(!stop) {
   Command nextCommand = commands.take();
   //do something with nextCommand
}

+2

Most likely, there is an error that you will not catch or handle correctly. Double check that you are not throwing Exceptions.

You are also trying to cut out program fragments that, in your opinion, are not related to each other, and make sure that they do not work. This will help you identify the true root cause.

0
source

Source: https://habr.com/ru/post/1786983/


All Articles