Android alarmmanager已同步

Android alarmmanager已同步,android,multithreading,synchronize,Android,Multithreading,Synchronize,我指的是 在这里,线程的可运行状态如下所示 Runnable mTask = new Runnable() { public void run() { Log.v("service", "thread is running after 5 min"); // Normally we would do some work here... for our sample, we will /

我指的是

在这里,线程的可运行状态如下所示

Runnable mTask = new Runnable() 
{        
    public void run() 
    {  
        Log.v("service", "thread is running after 5 min");
        // Normally we would do some work here...  for our sample, we will           
        // just sleep for 30 seconds.            
        long endTime = System.currentTimeMillis() + 15*1000;            
        while (System.currentTimeMillis() < endTime) 
        {                
            synchronized (mBinder) 
            {                    
                try 
                {                        
                    mBinder.wait(endTime - System.currentTimeMillis());      
                } 
                catch (Exception e) 
                {                    

                }                
            }            
        }            // Done with our work...  stop the service!            
        AlarmService_Service.this.stopSelf();     
    }    
}
我承认我对同步的概念有一些问题。。。线程运行while循环等待15秒,在该循环中我等待了15秒。那么,如果我只想写一个日志条目,例如log.vTAG,TEXT;,runnable看起来会是什么样子呢;?如果我想在自己的数据库表中写入一个新条目,会有什么变化

谢谢,A.

只需更换即可

试一试 { mBinder.waitendTime-System.currentTimeMillis; } 捕获异常e {

}

…使用要执行的代码


Synchronized只是声明一次只有一个进程访问该线程。

如果您只需要一条log语句,那么下面的操作就可以了

Runnable mTask = new Runnable() 
{        
    public void run() 
    {  
        Log.v("TAG", "Some verbose log message");
    }    
}
是否需要在对象上使用同步取决于对象是否是线程安全的。如果它不是线程安全的,那么您需要通过使用同步块确保一次只有一个线程访问该对象。在您的示例中,mBinder不是线程安全的,因此为了调用绑定器的wait方法,您需要确保您是唯一访问它的线程


runnable通常用于在不同的线程中执行代码,这样长时间运行的操作(如IO)就不会阻塞UI线程。

是和否,我有不需要的外部while循环,然后代码就在同步绑定器中。在上面的示例中,它们使用mBinder对象调用wait来执行一些操作。我不会对mBinder对象做任何事情,是吗?我需要同步吗?