同步说明符阻碍了可变数据的传输

时间:2013-04-16 18:14:31

标签: java synchronized

我最近组建了一个程序,通过USB端口连续获取数据并将其传输到COSM。通过serialevent方法连续获得的数据未传递给update方法,该方法用于在COSM上传上述数据。但是,从synchronized方法的签名中删除serialevent说明符后,此问题已得到解决。

public synchronized void serialEvent(SerialPortEvent oEvent)//Event handler for Serial communication  
{

    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) 
    {
        try {

            val=input.read();//Variable where data is received.Initialized to 0.
             this.update(78164);  

        } 
        catch (Exception e)
        {
            System.err.println(e.toString());
        }
    }

}

上传方法:

private  void update(int feedid) throws PachubeException, IOException ,Exception
{

    Feed f = this.pachube.getFeed(feedid);
   //System.out.println(f==null);
     System.out.println("updating ...");




     f.updateDatastream(8345, (double) val);//Expected value=77,Obtained value=0
     f.updateDatastream(6274, (double) val);//Expected value=77,Obtained value=0
     f.updateDatastream(1044, (double) val);//Expected value=77,Obtained value=0

   System.out.println("updated");

}

我的问题是synchronized说明符如何拒绝变量中存在的数据从serialevent方法传递到upload,即使我没有使用多个线程?

0 个答案:

没有答案