我在Java中使用线程的经验不多。如何从Runnable(){}内部获取String ft的值,以便在main()中使用它。
2017-12-16 17:21:33.539 -04:00 [DBG] Using Identity.Application as default scheme for authentication
2017-12-16 17:21:33.583 -04:00 [DBG] Using Identity.External as default scheme for sign-in
2017-12-16 17:21:33.585 -04:00 [DBG] Using Identity.External as default scheme for sign-out
2017-12-16 17:21:33.585 -04:00 [DBG] Using Identity.Application as default scheme for challenge
2017-12-16 17:21:33.586 -04:00 [DBG] Using Identity.Application as default scheme for forbid
2017-12-16 17:21:33.720 -04:00 [DBG] Starting token cleanup
2017-12-16 17:21:36.466 -04:00 [DBG] Request path /connect/authorize matched to endpoint type Authorize
2017-12-16 17:21:36.499 -04:00 [DBG] Endpoint enabled: Authorize, successfully created handler: IdentityServer4.Endpoints.AuthorizeEndpoint
2017-12-16 17:21:36.502 -04:00 [INF] Invoking IdentityServer endpoint: IdentityServer4.Endpoints.AuthorizeEndpoint for /connect/authorize
2017-12-16 17:21:36.514 -04:00 [DBG] Start authorize request
2017-12-16 17:21:36.531 -04:00 [DBG] No user present in authorize request
2017-12-16 17:21:36.539 -04:00 [DBG] Start authorize request protocol validation
2017-12-16 17:21:37.900 -04:00 [DBG] mvc found in database: true
2017-12-16 17:21:37.915 -04:00 [DBG] Checking for PKCE parameters
2017-12-16 17:21:37.917 -04:00 [DBG] No PKCE used.
2017-12-16 17:21:38.017 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:21:38.345 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:21:38.370 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:21:38.382 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:21:38.398 -04:00 [DBG] Calling into custom validator: IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator
2017-12-16 17:21:38.495 -04:00 [INF] ValidatedAuthorizeRequest
{
"ClientId": "mvc",
"ClientName": "MVC Client",
"RedirectUri": "http://localhost:5002/signin-oidc",
"AllowedRedirectUris": [
"http://localhost:5002/signin-oidc"
],
"SubjectId": "anonymous",
"ResponseType": "code id_token",
"ResponseMode": "form_post",
"GrantType": "hybrid",
"RequestedScopes": "openid profile api1 offline_access",
"State": "CfDJ8OjFhWHnTLBEporSKt_A6SqBDQbwqg26gGARan3UYPBz6RCfh0BazL0IWdxWjjgzJU7Qknp01KHWRMy_VSdJ8H7Jbbwsxhj2c5JZ8Jk5NFb-EASixxCCE3UH19kdWu9H_MpW33PqRVIAkQGuk7lVLmaRa85bWMYEMIh8N5zUbXjDCi90qj_lokqwxMmlZ3avfgVo3NaTEGh94CZzvEa5I33D2WbSx1h4xA2SVOZqJNWZESWCQazUuQt8745dUVe0fx8fecSDBlZJK2F_QNh5I-6FPw7uCrlZUKYNhbkWUdyvNazMIVQIrxq8RUcjgfL-_cYIVyPi5cVUPtHg90uuNJ4",
"Nonce": "636490560962891702.OGQ5ZjE0OGQtN2Y1My00NzRhLTg0MjktZWFkMTU2YmViODhjMDk2YTc0M2YtNGY1MC00M2JiLTlhNjQtN2RjMjE1NDZmYzJh",
"Raw": {
"client_id": "mvc",
"redirect_uri": "http://localhost:5002/signin-oidc",
"response_type": "code id_token",
"scope": "openid profile api1 offline_access",
"response_mode": "form_post",
"nonce": "636490560962891702.OGQ5ZjE0OGQtN2Y1My00NzRhLTg0MjktZWFkMTU2YmViODhjMDk2YTc0M2YtNGY1MC00M2JiLTlhNjQtN2RjMjE1NDZmYzJh",
"state": "CfDJ8OjFhWHnTLBEporSKt_A6SqBDQbwqg26gGARan3UYPBz6RCfh0BazL0IWdxWjjgzJU7Qknp01KHWRMy_VSdJ8H7Jbbwsxhj2c5JZ8Jk5NFb-EASixxCCE3UH19kdWu9H_MpW33PqRVIAkQGuk7lVLmaRa85bWMYEMIh8N5zUbXjDCi90qj_lokqwxMmlZ3avfgVo3NaTEGh94CZzvEa5I33D2WbSx1h4xA2SVOZqJNWZESWCQazUuQt8745dUVe0fx8fecSDBlZJK2F_QNh5I-6FPw7uCrlZUKYNhbkWUdyvNazMIVQIrxq8RUcjgfL-_cYIVyPi5cVUPtHg90uuNJ4",
"x-client-SKU": "ID_NET",
"x-client-ver": "2.1.4.0"
}
}
2017-12-16 17:21:38.516 -04:00 [INF] Showing login: User is not authenticated
2017-12-16 17:21:38.742 -04:00 [INF] AuthenticationScheme: Identity.External signed out.
2017-12-16 17:21:38.759 -04:00 [DBG] Start authorize request protocol validation
2017-12-16 17:21:38.771 -04:00 [DBG] mvc found in database: true
2017-12-16 17:21:38.771 -04:00 [DBG] Checking for PKCE parameters
2017-12-16 17:21:38.774 -04:00 [DBG] No PKCE used.
2017-12-16 17:21:38.781 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:21:38.791 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:21:38.801 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:21:38.810 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:21:38.812 -04:00 [DBG] Calling into custom validator: IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator
2017-12-16 17:22:00.441 -04:00 [DBG] Augmenting SignInContext
2017-12-16 17:22:00.455 -04:00 [INF] AuthenticationScheme: Identity.Application signed in.
2017-12-16 17:22:00.461 -04:00 [INF] User logged in.
2017-12-16 17:22:00.473 -04:00 [DBG] Request path /connect/authorize/callback matched to endpoint type Authorize
2017-12-16 17:22:00.477 -04:00 [DBG] Endpoint enabled: Authorize, successfully created handler: IdentityServer4.Endpoints.AuthorizeCallbackEndpoint
2017-12-16 17:22:00.479 -04:00 [INF] Invoking IdentityServer endpoint: IdentityServer4.Endpoints.AuthorizeCallbackEndpoint for /connect/authorize/callback
2017-12-16 17:22:00.489 -04:00 [DBG] Start authorize callback request
2017-12-16 17:22:00.495 -04:00 [DBG] No user present in authorize request
2017-12-16 17:22:00.496 -04:00 [DBG] Start authorize request protocol validation
2017-12-16 17:22:00.510 -04:00 [DBG] mvc found in database: true
2017-12-16 17:22:00.510 -04:00 [DBG] Checking for PKCE parameters
2017-12-16 17:22:00.515 -04:00 [DBG] No PKCE used.
2017-12-16 17:22:00.527 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:22:00.537 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:22:00.544 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:22:00.554 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:22:00.556 -04:00 [DBG] Calling into custom validator: IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator
2017-12-16 17:22:00.562 -04:00 [INF] ValidatedAuthorizeRequest
{
"ClientId": "mvc",
"ClientName": "MVC Client",
"RedirectUri": "http://localhost:5002/signin-oidc",
"AllowedRedirectUris": [
"http://localhost:5002/signin-oidc"
],
"SubjectId": "anonymous",
"ResponseType": "code id_token",
"ResponseMode": "form_post",
"GrantType": "hybrid",
"RequestedScopes": "openid profile api1 offline_access",
"State": "CfDJ8OjFhWHnTLBEporSKt_A6SqBDQbwqg26gGARan3UYPBz6RCfh0BazL0IWdxWjjgzJU7Qknp01KHWRMy_VSdJ8H7Jbbwsxhj2c5JZ8Jk5NFb-EASixxCCE3UH19kdWu9H_MpW33PqRVIAkQGuk7lVLmaRa85bWMYEMIh8N5zUbXjDCi90qj_lokqwxMmlZ3avfgVo3NaTEGh94CZzvEa5I33D2WbSx1h4xA2SVOZqJNWZESWCQazUuQt8745dUVe0fx8fecSDBlZJK2F_QNh5I-6FPw7uCrlZUKYNhbkWUdyvNazMIVQIrxq8RUcjgfL-_cYIVyPi5cVUPtHg90uuNJ4",
"Nonce": "636490560962891702.OGQ5ZjE0OGQtN2Y1My00NzRhLTg0MjktZWFkMTU2YmViODhjMDk2YTc0M2YtNGY1MC00M2JiLTlhNjQtN2RjMjE1NDZmYzJh",
"Raw": {
"client_id": "mvc",
"redirect_uri": "http://localhost:5002/signin-oidc",
"response_type": "code id_token",
"scope": "openid profile api1 offline_access",
"response_mode": "form_post",
"nonce": "636490560962891702.OGQ5ZjE0OGQtN2Y1My00NzRhLTg0MjktZWFkMTU2YmViODhjMDk2YTc0M2YtNGY1MC00M2JiLTlhNjQtN2RjMjE1NDZmYzJh",
"state": "CfDJ8OjFhWHnTLBEporSKt_A6SqBDQbwqg26gGARan3UYPBz6RCfh0BazL0IWdxWjjgzJU7Qknp01KHWRMy_VSdJ8H7Jbbwsxhj2c5JZ8Jk5NFb-EASixxCCE3UH19kdWu9H_MpW33PqRVIAkQGuk7lVLmaRa85bWMYEMIh8N5zUbXjDCi90qj_lokqwxMmlZ3avfgVo3NaTEGh94CZzvEa5I33D2WbSx1h4xA2SVOZqJNWZESWCQazUuQt8745dUVe0fx8fecSDBlZJK2F_QNh5I-6FPw7uCrlZUKYNhbkWUdyvNazMIVQIrxq8RUcjgfL-_cYIVyPi5cVUPtHg90uuNJ4",
"x-client-SKU": "ID_NET",
"x-client-ver": "2.1.4.0"
}
}
2017-12-16 17:22:00.570 -04:00 [INF] Showing login: User is not authenticated
2017-12-16 17:22:00.580 -04:00 [INF] AuthenticationScheme: Identity.External signed out.
2017-12-16 17:22:00.581 -04:00 [DBG] Start authorize request protocol validation
2017-12-16 17:22:00.591 -04:00 [DBG] mvc found in database: true
2017-12-16 17:22:00.592 -04:00 [DBG] Checking for PKCE parameters
2017-12-16 17:22:00.593 -04:00 [DBG] No PKCE used.
2017-12-16 17:22:00.601 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:22:00.610 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:22:00.617 -04:00 [DBG] Found ["openid","profile"] identity scopes in database
2017-12-16 17:22:00.626 -04:00 [DBG] Found ["api1"] API scopes in database
2017-12-16 17:22:00.627 -04:00 [DBG] Calling into custom validator: IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator
2017-12-16 17:22:03.763 -04:00 [INF] Clearing 0 tokens
2017-12-16 17:22:07.013 -04:00 [DBG] TaskCanceledException. Exiting.
2017-12-16 17:22:07.040 -04:00 [DBG] Stopping token cleanup
编译时的结果:
public class testInner {
public static void main (String[] args){
Outer out =new Outer();
out.makeinner();
System.out.println("main :" + out.getft());
}
}
public class Outer {
private volatile String ft;
public String getft(){
return ft;
}
public void makeinner(){
inner in = new inner();
in.changeOuter();
}
public class inner{
public void changeOuter(){
new Thread(new Runnable(){
public void run(){
ft = "what?";
System.out.println("run :" + ft);
}
}).start();
}
}
}
答案 0 :(得分:2)
线程是并行执行的,因此在执行System.out.println("main :" + out.getft());
时无法确定线程是否已成功执行。这就是为什么你得到null
,线程还没有编辑那个变量。
如果您想确保线程完成执行,请使用join
:
public class inner{
public void changeOuter(){
Thread myThread = new Thread(new Runnable(){
public void run(){
ft = "what?";
System.out.println("run :" + ft);
}
});
myThread.start();
myThread.join(); // wait for the thread to finish before returning
}
}