Can declarative services be used in a thread?

综合编程 Hello, buddy! (源链)

I have the following code as an OSGi module.

When it runs, I get the message that the logger has been set:

UdpListener > setStoreLog: '[email protected]'

But immediatly after that, the loop in the run() function says that storeLog is empty

ERROR > UdpListener > run > storeLog is not available.

Any ideas what could be wrong?

Could it be the fact that this is running in a thread?

package com.mine.logger.internal.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;

import com.mine.logger.storeindb.IStoreLog;

public class UdpListener extends Thread
{
    private int port;

    private IStoreLog storeLog;

    public void setStoreLog(IStoreLog value)
    {
        this.storeLog = value;
        System.out.println("UdpListener > setStoreLog: '" + this.storeLog.toString() + "'");
    }

    public void unsetStoreLog(IStoreLog value)
    {
        if (this.storeLog == value) {
            this.storeLog = null;
        }
        System.out.println("UdpListener > unsetStoreLog");
    }

    public UdpListener()
    {
        // public, no-args constructor needed for Declarative Services !!!
    }

    public UdpListener(int port)
    {
        this.port = port;
    }

    public void run()
    {
        startListener();
    }

    private void startListener()
    {
        try {
            // send command
            DatagramSocket socket = new DatagramSocket(port);

            while (true)
            {
                byte[] b = new byte[1000];
                DatagramPacket recvdPacket = new DatagramPacket(b, b.length);
                socket.receive(recvdPacket);

                System.out.println("UdpListener: Packet received. " + (new String(b)));

                try
                {
                    if (this.storeLog != null)
                        this.storeLog.doStore(new Date(), InetAddress.getByName("0.0.0.0"), port, 1, "UDP", b);
                    else
                        System.err.println("ERROR > UdpListener > run > storeLog is not available.");
                }
                catch (Exception e)
                {
                    System.err.println("ERROR > UdpListener > run > storeLog > Exception: " + e.toString());
                }
            }
        } catch (SocketException e) {
            System.out.println("ERROR > UdpListener > run > SocketException: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("ERROR > UdpListener > run > IOException: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("ERROR > UdpListener > run > Exception: " + e.getMessage());
        }
    }
}

Your code is not thread safe. The storeLog field is read and written by multiple threads without any synchronization. If you have a mutable field that is read and written by more than one thread, you must ensure the field is always safely accessed for both
read and write. I highly
recommend the excellent book Java Concurrency in Practice http://www.javaconcurrencyinpractice.com/
for anyone writing Java code.

您可能感兴趣的

5 Annotations Every Java Developer Should Know Since their inception in Java Development Kit (JDK) 5, annotations have become an indispensable part of the Java ecosystem. While there are countless...
Java四种引用对比 老婆保佑,代码无BUG 前言 面试题:Java中的软引用,弱引用在Android 是哪个的使用 目录 一:哪四种引用 二:区别在哪 三:在Android中的使用场景 Handler 弱引用,...
面试必问!Java 多线程中两个线程交替执行,一个输出偶数,一个输出奇数... 前言 楼主今天在面经上看到这个题,挺有意思,小小的题目对多线程的考量还挺多。大部分同学都会使用 synchronized 来实现。楼主今天带来另外两种优化实现,让你面试的时候,傲视群雄! 第一种 synchronized class ThreadPrintDemo2 { pub...
java替换Excel字符 //模板路径 String modelPath="D:Excel.xls" //sheet的名字 String sheetName="sheet1"; 获取Excel模板对象 try { File file = new File(modelPath); if(!file.exists()){...
Java程序师面试失败的最直接的原因原来是这样的... 在进行面试的时候每个人可能都会有一些失败的教训吧,共同探讨的一些常见的失败的原因。 一、说得太多 不断地说,不断地说,却并没有什么实质性的内容。换句话说,就是废话连篇,言之无物。如果你不能简洁的解释问题,那么面试官就会怀疑你在工作时的表现是不是也会像你的谈话一样拖泥带水?可以先问问面试官,确定...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Can declarative services be used in a thread?



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录