Hi,
The following Function is executing in its own thread:
private void doSendData()
{
try {
//writeToFile(); // just a temporary location of a call
InetAddress serverAddr = InetAddress.getByName(serverAddress);
serverAddr.wait(60000);
//Log.d("TCP", "C: Connecting...");
Socket socket = new Socket(serverAddr, portNumber);
socket.setSoTimeout(3000);
try {
//Log.d("TCP", "C: Sending: '" + message + "'");
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
String message = packData();
out.println(message);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
connectionAvailable = true;
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
connectionAvailable = false;
} finally {
socket.close();
announceNetworkAvailability(connectionAvailable);
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
announceNetworkAvailability(connectionAvailable);
}
}
When the execution reaches the line serverAddr.wait(60000) it throws an exception:
java.lang.IllegalMonitorStateException: object not locked by thread before wait()
does anyone know how to lock an object or a function in order to prevent the concurrency?
I've tried to add a Lock object:
private final Lock lock = new ReentrantLock();
and the line
boolean locked = lock.tryLock();
at the beginning of function but it didn't work.
Thanks for help!