何时ThreadLocal优先于局部变量?(When is ThreadLocal preferred over Local Variables?)
Java中的
ThreadLocal
说:Java中的ThreadLocal类使您可以创建只能由同一线程读取和写入的变量。 因此,即使两个线程正在执行相同的代码,并且代码具有对ThreadLocal变量的引用,那么这两个线程也看不到彼此的ThreadLocal变量。
我的问题是:当我们需要获取一个特定于线程的变量时,我们不能只将该变量声明为方法中的局部变量吗? 因为每个线程都有自己的堆栈,因此它获得了自己的变量副本。 我在这里错过了什么吗?
ThreadLocal
in Java says that:The ThreadLocal class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal variable, then the two threads cannot see each other's ThreadLocal variables.
My question is: When we need to get a variable specific to a thread, can't we just declare that variable as a local variable inside a method? Because every thread has its own stack and thus it gets its own copy of variables. Am I missing something here?
原文:https://stackoverflow.com/questions/38448292
最满意答案
最好的解决方案是在服务器端安装denver并使用PHP代码。
这是js文件中的代码。
deleteNewsItem: function(newsId, callback, scope) { var dataPath = this.dataPath; $.getJSON(dataPath + '/delete-news.php?id='+newsId, function(rawHeadlines) { var headlines = []; $.each(rawHeadlines, function(index, rawHeadline) { headlines.push(rawHeadline.id); } }); if (callback) { callback.call(scope, headlines); } };
这是“delete_news.php”文件中的代码。
<?php $deleteId = isset($_GET['id'])?$_GET['id']:false; if ($deleteId){ $content = file_get_contents('delete-news.json'); $json = json_decode($content); $newData = array(); foreach($json as $key=>$id){ $newData []= (array)$id; } $newData []= array("id"=>(int)$deleteId); $fill_view = json_encode($newData); file_put_contents('delete-news.json', ($fill_view)); echo $fill_view; } else { echo json_encode(array()); } exit(); ?>
The best solution was to install denver and use PHP code on server-side.
This is code in the js file.
deleteNewsItem: function(newsId, callback, scope) { var dataPath = this.dataPath; $.getJSON(dataPath + '/delete-news.php?id='+newsId, function(rawHeadlines) { var headlines = []; $.each(rawHeadlines, function(index, rawHeadline) { headlines.push(rawHeadline.id); } }); if (callback) { callback.call(scope, headlines); } };
This is the code in the "delete_news.php" file.
<?php $deleteId = isset($_GET['id'])?$_GET['id']:false; if ($deleteId){ $content = file_get_contents('delete-news.json'); $json = json_decode($content); $newData = array(); foreach($json as $key=>$id){ $newData []= (array)$id; } $newData []= array("id"=>(int)$deleteId); $fill_view = json_encode($newData); file_put_contents('delete-news.json', ($fill_view)); echo $fill_view; } else { echo json_encode(array()); } exit(); ?>
相关问答
更多-
我想到了... 这可能会帮助像我一样的新手,他们犯了同样的错误,所以我会回答我自己的问题:) 原来我没有登录,去图。 虽然这个回应很奇怪,并且帮助我抛弃了整个主页。 原因是我阻止了我的函数文件,其中包含Ajax响应者,如下所示: function block_users() { if( !current_user_can( 'delete_pages' ) ) { wp_redirect( get_home_url(), 301 ); exit; } } ...
-
我终于找到了问题的解决方案,这个问题很简单但完全不明显。 事实证明,我试图反序列化的Model对象是在一个名称不正确的包中,当Spring无法找到Model时,它只是吞下生成的异常并返回false。 我通过在Spring-MVC中深入调试,特别是StdDeserializerProvider类来发现这一点。 对于那些收到此错误的人,我强烈建议编写一些代码来验证此类中发生的情况,例如: @Test public void testThatCanConvertUpdateModel() { MappingJ ...
-
在发布复杂数据时,您应该在将contentType指定为application/json同时发送对象的JSON字符串化版本 您可以使用JSON.stringify方法将数组转换为相应的json字符串。 var data = []; data.push({ Company: "Microsoft", Location: "Redmond" , Total:123 }); data.push({ Company: "UrbanScience", Location:"Detroit", Total:456 }); ...
-
最好的解决方案是在服务器端安装denver并使用PHP代码。 这是js文件中的代码。 deleteNewsItem: function(newsId, callback, scope) { var dataPath = this.dataPath; $.getJSON(dataPath + '/delete-news.php?id='+newsId, function(rawHeadlines) { var headlines = []; $. ...
-
allFunctions.php: if ($_POST['candy']) { allFunctions::giveCandy(); } class allFunctions { static function giveCandy () { $db = Database::getInstance(); // .... JS: $(document).ready(function () { $('#button_candy').subm ...
-
正如@georgeawg建议的那样,您可以使用$ http服务: $http.post('/someUrl', data, config).then(successCallback, errorCallback); 并修改你的代码为: $http.post({ 'api/pushnotification', // Post URL JSON.stringify(item), // Data to be sent contentType: 'application/ ...
-
127.0.0.1 - - [04/Jan/2017 09:13:47] "OPTIONS /add_profile_data HTTP/1.1" 500 OPTIONS请求不包含JSON。 编辑代码以仅在方法为POST检查JSON。 例如: @app.route('/add_profile_data', methods=['POST', 'GET']) def add(): if request.method == 'POST': data = request.json ...
-
如果您不希望返回JSON格式,则可能需要删除它。 submit(){ if(this.checkRequiredField()){ $.ajax({ url: '/api/Accounts', type: 'POST', data: { Name: this.state.name, StartDate :this.state.startDate, EndD ...
-
最后解决了它,当你从控制器返回结果而不是返回JSON我使用了“内容(值)”。 控制器代码: public ActionResult ImportExcelData(HttpPostedFileBase UploadExcel) { try { var Data = Repository.ImportData(filePath, OrgID); if(sData!=null) { ...
-
问题是你的函数functionRedeempoints不会返回ajax调用可以处理的任何内容。 它甚至在返回声明之前就已经消亡了。 此外,PHP端的返回将永远不会被JS解释。 JS只能读取http请求,因此您需要通过echo语句实际写入它。 Wordpress为您提供了一种方便的处理方式:您需要的是: function functionRedeempoints() { wp_send_json_success(true); } 这已经负责停止执行并正确地对您的响应进行JSON编码。 JS方面的正确响 ...