Java中的线程:如何锁定对象? [英] Threading in Java: How to lock an object?

查看:187
本文介绍了Java中的线程:如何锁定对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下函数在自己的线程中执行:

  private void doSendData()
{
try {

// writeToFile(); //只是一个调用的临时位置
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);
}
}

当执行到 serverAddr.wait(60000)它抛出异常:

  java.lang.IllegalMonitorStateException:对象未被线程锁定wait()

有人知道如何锁定一个对象或一个函数,以防止并发?
我试图添加一个Lock对象:

  private final Lock lock = new ReentrantLock 

和行

  boolean locked = lock.tryLock(); 

在函数的开头,但它不工作。


<为了在一个对象上调用wait(),你必须保持该对象的同步锁(虽然锁实际上是在线程等待的时候释放的) :

 同步(serverAddr){
serverAddr.wait
}

我不得不承认希望这样做在这种情况下阻碍我...


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.

解决方案

In order to call wait() on an object, you have to hold the synchronized lock on that object (though the lock is actually released while the thread is waiting):

synchronized (serverAddr) {
  serverAddr.wait();
}

I have to admit that why you're wanting to do this baffles me in this case...

这篇关于Java中的线程:如何锁定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆