可以在Java中简化此并发代码(Can this concurrent code be simplified in Java)
我有以下用例:我的Android应用程序向服务器发出多个并发HTTP请求。 如果请求产生“不正确的令牌”错误,则应用程序应通过向服务器发出特殊请求并重复失败的请求来重新生成令牌。
因为有多个并发请求产生相同的错误,所以只有在使用以下构造时才应生成令牌:
static final AtomicBoolean isTokenSync = new AtomicBoolean(false); private static CountDownLatch countDownLatchTokenSync; if (isTokenSync.compareAndSet(false, true)) { countDownLatchTokenSync = new CountDownLatch(1); generateAndSaveToken(...); isTokenSync.set(false); countDownLatch.countDown(); } else { try { countDownLatch.await(5, TimeUnit.MINUTES); } catch (InterruptedException e) { } } } // repeat the request
基本上,第一个线程通过并生成令牌,其他线程等待,直到令牌生成并继续。 我的代码是实现这种行为的好方法吗? 可以简化吗?
I have the following use case: my Android application makes multiple concurrent HTTP requests to the server. If a request produces "Incorrect token" error then the application should regenerate the token by making a special request to the server and repeat the failed request.
As there are multiple concurrent requests that produce the same error and token should be generated only once I used the following construct:
static final AtomicBoolean isTokenSync = new AtomicBoolean(false); private static CountDownLatch countDownLatchTokenSync; if (isTokenSync.compareAndSet(false, true)) { countDownLatchTokenSync = new CountDownLatch(1); generateAndSaveToken(...); isTokenSync.set(false); countDownLatch.countDown(); } else { try { countDownLatch.await(5, TimeUnit.MINUTES); } catch (InterruptedException e) { } } } // repeat the request
Basically first thread gets through and generates the token and other threads wait until token generated and proceed. Is my code a good approach to achieve the behavior? Can it be simplified?
原文:https://stackoverflow.com/questions/33831419
最满意答案
您的链接需要以“http://”开头,以便将其识别为外部URL。
你可以这样做:
string extLink = @Model[i].Url_Address[x]; if(!extLink.StartsWith(@"http://")) { extLink = @"http://" + extLink; }
......
<a href="@extLink" class="list-group-item">@Model[i].Url_Name[x]</a>
注意:这仅用于演示目的,有更多的最佳方法(即在服务器端执行此操作),但您应该明白这一点。
Your link needs to start with "http://" for it to recognise this as an external URL.
You could do something like:
string extLink = @Model[i].Url_Address[x]; if(!extLink.StartsWith(@"http://")) { extLink = @"http://" + extLink; }
......
<a href="@extLink" class="list-group-item">@Model[i].Url_Name[x]</a>
Note: This is for demo purposes only, there are more optimal ways to do this (i.e. do this on the server side), but you should get the idea.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
Chrome中的MVC重定向错误(MVC Redirect error in Chrome)[2021-04-08]
在加载页面之前,我使用chrome简单地显示了上述错误,我遇到了相同的行为。 在处理由使用Windows身份验证导致的http2-> http1.1降级时( HTTP2不支持Windows身份验证 ),我将其缩小为chrome问题。 我最终通过在服务器上禁用http2来解决该问题。 在我的情况下没有真正的缺点,因为所有的网站都支持Windows身份验证,所以无论如何连接总是被降级到http1.1。 以下铬错误有更多细节。 I ran into the same behavior with chrome br ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
mvc 重定向到其他网站?(mvc redirect to other website?)[2023-08-23]
您的链接需要以“http://”开头,以便将其识别为外部URL。 你可以这样做: string extLink = @Model[i].Url_Address[x]; if(!extLink.StartsWith(@"http://")) { extLink = @"http://" + extLink; } ...... @Model[i].Url_Name[x] 注意:这仅用于演示目的,有更多的最 ... -
关于重定向: 首先在您的会话中存储用户是否访问过欢迎页面。 然后在您的主索引操作中检查此值,如果为false,则将RedirectToAction返回给您欢迎页面。 在欢迎页面操作中,更新您的会话以“记住”该用户访问此页面。 关于重定向回主页: 你可以通过在视图中添加一些javasctipt来实现它: $(document).ready(function() { setTimeout(function() { window.location.href = "@Url.Action("In ...
-
您可以将自己附加到DocumentCompleted事件,它会在文档完全加载时触发,然后您可以以类似的方式搜索DOM。 首先,在构造函数中向DocumentCompleted事件添加事件处理程序,并在Form关闭时删除事件处理程序。 在load事件中,您可以导航到所需页面,然后在DOM中搜索所有li,然后检查所需的文本。 如果匹配,则搜索URL,然后调用click事件(这将触发您的javascript Goto功能)。 public partial class Form1 : Form { publ ...
-
-
Href到Onclick重定向(Href to Onclick redirect)[2022-09-25]
你在寻找AJAX。 这允许您在不实际导航到页面的情况下发出请求。 因为你正在使用jQuery $("#emailSubscribe").click(function (e) { e.preventDefault(); $.get("http://www.myurl.com", function (data) { //All done! }); }); You're looking for AJAX. This allows you to make requests without ... -
你应该使用这段代码: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new {area="fa", controller = "Home", action = "Index ...
-
任何没有协议( http:// )或正斜杠( / )的URL都被视为相对URL。 如果它以www开头并不重要,那只是被认为是一个任意的子域。 由于它被认为是相对的,因此它会附加到现有URL结尾的开头。 因此,为了重定向网站,您需要一个完全限定的绝对URL。 Any URL that doesn't have a protocol (http://) or a forward slash (/) is considered a relative URL. It doesn't matter if it sta ...