Problem Summary
I've got an application which combines both Java and Python, the two languages communicating across a localhost socket. (I cannot use Jython, I can't throw out Java for PyGame, and I can't throw out Python for pure Java.)
The application opens the socket between Python and Java and a socket from Java to a central server. Input to Java from the UI or from the server is passed on to Python, where the program logic is taken care of. Python then sends messages back to Java which are either passed on to the server, or are interpreted for use on the client (eg, Python tells Java to draw something to the screen).
Code Snippets
Client.java:main
PyConnect python = new PyConnect();
Thread th = new Thread(python);
th.start();
while(!python.isInitialized());
Client game = new Client();
PythonMessageDecoder.setGame(game);
th = new Thread(game);
th.start();
PyConnect.java:run
System.out.println("Starting...");
// ...Initialize ServerSocket instance to Python...
try
{
// ...Accept connection from Python...
System.out.println("Connected!!!");
}
// ...catch...
try
{
// ...Get input/output streams...
System.out.println("Both Streams are up");
}
// ...catch...
initialized = true;
while(PythonMessageDecoder.getInstance() == null);
// ...Read from Python...
PythonMessageDecoder.java
public static void setGame(Game game) { PythonMessageDecoder.game = game; }
public static PythonMessageDecoder getInstance()
{
if(game == null) return null;
return InstanceHolder.INSTANCE;
}
// ...
Problem Detail
On all of our Windows machines (XP, Vista, and W7; x86 and x64), this process runs fine. On our Ubuntu and OSX machines, there are no exceptions (in either Python or Java)... but no Java frame appears. In my samples above, I've left in trace debugging, and the Mac and Linux boxes print:
Starting...
Connected!!!
Both Streams are up
Additionally, the other Java portions of our project (which don't try to combine with Python) work perfectly fine. The Python shell works fine. The Python code runs just fine (if the Python didn't connect, PyConnect
would not reach the 'Connected!!!' line).
Solution Attempts
At first, I thought the issue might have been deadlock between the two threads, as they both stop to wait for a condition before completing their task. But PyConnect
sets its initialized
state before waiting, and the main thread is checking that PyConnect
has reached its initialized
state. To be honest, I'm not certain what the issue is.