变量在我的单例对象中改变状态

时间:2014-05-07 09:29:43

标签: java singleton

我负责启动和停止http服务器的单个对象(使用RESTlet框架)。

创建对象时,boolean var isStarted设置为false。启动投放后,isStarted设置为true

这是我的单身人士代码:

private WebService() {
    this(Protocol.HTTP, DEFAULT_PORT);
}

private WebService(final Protocol protocol, final int port)
{
    this.protocol = protocol;
    this.port = port;
    this.isStarted = false;

    // Creating a new Restlet component and 
    this.component = new Component();
}

public synchronized static WebService getInstance()
{
    if(instance == null)
    {
        return new WebService();
    }

    return instance;
}


public synchronized static WebService getInstance(final Protocol protocol, final int port)
{
    if(instance != null)
    {
        instance = null;
    }

    return new WebService(protocol, port);
}

现在我只使用getInstance(),没有参数。

以下是我启动和停止服务器的方法:

public synchronized boolean startServer(final Context context)
{
    if ( isStarted ) {
        Log.d(TAG, "Tried to start a running server");
        throw new IllegalStateException("Tried to start a running server");
    }

    // Add a http connector to the component.
    component.getServers().add(protocol, port);
    component.getClients().add(Protocol.FILE);  

    // Static content
    // Attach the component to the localhost
    ipAuthenticator = new IPAuthenticator(org.restlet.Context.getCurrent());
    ContactsRouter contactsRouter = new ContactsRouter(context, ipAuthenticator);
    AuthenticationRouter authenticationRouter = new AuthenticationRouter(contactsRouter, ipAuthenticator);
    component.getDefaultHost().attach(authenticationRouter);

    // Start it! The HTTP server connector is also automatically started. 
    try {
        component.start();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        isStarted = false;
        return false;
    }

    isStarted = true;
    Log.d(TAG, "Server Started! and isStarted is: " + isStarted);

    return true;
}

public Boolean stopServer()
{
    Log.d(TAG, "WebService: isStarted: " + isStarted);
    if(isStarted)
    {
        try {
            component.stop();
            Log.d(TAG, "Server stopped");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Exception - Could not stop the server!");
            e.printStackTrace();
        }
    }

    return false;
}

我的问题是,当我使用我的应用程序用户界面启动和停止服务器时,我可以毫无问题地启动它,但是当我尝试停止服务器时(启动后)我不能因为{{1当它应该是isStarted时,它是false,因为我刚刚启动了服务器。

以下是我尝试使用我的应用Uuser界面启动/停止服务器时:

true

同样,我的问题是,在服务器启动后,为什么它应该为真时为public HttpServer(ProgressBar progressBar, Context context, boolean isChecked) { this.progressBar = progressBar; progressBar.setEnabled(false); this.context = context; webService = WebService.getInstance(); this.isChecked = isChecked; } (...) @Override protected void onPostExecute(Void result) { // Hide the progress bar progressBar.setVisibility(View.GONE); // Here in this logcat I receive-> is sever started?: **false** but it should be true Log.d(TAG, "isChecked = " + isChecked + " || Is the server started?: " + webService.isStarted()); if(isChecked == true) { Log.d(TAG, "Going to start the server"); webService.startServer(context); } else { Log.d(TAG, "Going to stop the server"); webService.stopServer(); } } 为假?

谢谢!

1 个答案:

答案 0 :(得分:4)

您没有影响单件对象

public synchronized static WebService getInstance()
    {
        if(instance == null)
            instance = new WebService();
        return instance;
    }