gpt4 book ai didi

java - 变量改变我的单例对象内的状态

转载 作者:行者123 更新时间:2023-12-01 18:32:31 25 4
gpt4 key购买 nike

我有一个单例对象负责启动和停止 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;
}

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

这是当我尝试使用我的应用程序 Uuser 界面启动/停止服务器时:

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();
}
}

我的问题是,为什么在服务器启动后,isChecked 应该为 true 时却为 false?

谢谢!

最佳答案

您不会影响单例对象

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

关于java - 变量改变我的单例对象内的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23513944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com