{"id":3335,"date":"2018-11-08T16:42:38","date_gmt":"2018-11-08T08:42:38","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3335"},"modified":"2018-11-08T16:42:38","modified_gmt":"2018-11-08T08:42:38","slug":"vector-%e7%9c%9f%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3335","title":{"rendered":"Vector \u771f\u7684\u7ebf\u7a0b\u5b89\u5168\u7684\u5417\uff1f"},"content":{"rendered":"<p>\n\t\u4f60\u8fd8\u5728\u9762\u8bd5\u4e2d\u56de\u7b54&nbsp;Vector \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5417\uff1f\u6211\u76f8\u4fe1\u5f88\u591a\u4eba\u5728\u9762\u8bd5\u4e2d\u90fd\u662f\u8fd9\u6837\u56de\u7b54\u7684\uff0c\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5173\u4e8e Vector \u6211\u4eec\u4eca\u5929\u6765\u91cd\u65b0\u8ba4\u8bc6\u5b83\uff01\n<\/p>\n<p>\n\t\u5982\u679c\u4f60\u8bf4 Vector \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4e00\u5b9a\u662f\u57fa\u4e8e\u5b83\u7684\u65b9\u6cd5 synchronized \u5173\u952e\u5b57\u7684\u6240\u5f71\u54cd\u7684\uff0c\u5ba2\u89c2\u7684\u8ba4\u4e3a\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff01\n<\/p>\n<p>\n\t\u4f46\u5b9e\u9645\u4e0a&nbsp;Vector \u5728\u590d\u5408\u64cd\u4f5c\u4e0b\uff0c\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Vector \u96c6\u5408\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/vector.png\" style=\"width: 446px;height: 343px\" \/>\n<\/p>\n<p>\n\t\u4ec0\u4e48\uff1f\u600e\u4e48\u53ef\u80fd\uff0c\u52a0\u4e86 synchronized \u5173\u952e\u5b57\u8fd8\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5c0f\u7f16\u4f60\u4e00\u5b9a\u662f\u5728\u9a97\u6211\u5bf9\u4e0d\u5bf9\uff1f\n<\/p>\n<p>\n\t\u8bf4\u5b83\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u662f\u5728\u540c\u6b65\u5bb9\u5668\u548c\u5e76\u53d1\u5bb9\u5668\u7684\u89d2\u5ea6\u6765\u8bf4\u7684\u3002\n<\/p>\n<p>\n\t\u5149\u8bf4\u4e0d\u7ec3\u662f\u5047\u628a\u5f0f\uff01\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e00\u6bb5\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static Object getLast(Vector list) {  \r\n    int lastIndex = list.size() - 1;  \r\n    return list.get(lastIndex);  \r\n} \r\npublic static void deleteLast(Vector list) {  \r\n    int lastIndex = list.size() - 1;  \r\n    list.remove(lastIndex);  \r\n} <\/pre>\n<p>\n\t\u4e0a\u9762\u7684\u4ee3\u7801\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u6216\u8005\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u4f7f\u7528\u4e86\u540c\u6b65\u5bb9\u5668Vector\u3002\u5982\u679c\u6709\u4e24\u4e2a\u7ebf\u7a0bA,B\u540c\u65f6\u8c03\u7528\u4e0a\u9762\u7684\u4e24\u4e2a\u65b9\u6cd5\uff0c\u5047\u8bbelist\u7684\u5927\u5c0f\u4e3a10\uff0c\u8fd9\u91cc\u8ba1\u7b97\u5f97\u5230\u7684lastIndex\u4e3a9\uff0c\u7ebf\u7a0bB\u9996\u5148\u6267\u884c\u4e86\u5220\u9664\u64cd\u4f5c\uff08\u591a\u7ebf\u7a0b\u4e4b\u95f4\u64cd\u4f5c\u6267\u884c\u7684\u4e0d\u786e\u5b9a\u6027\u5bfc\u81f4\uff09\uff0c\u800c\u540e\u7ebf\u7a0bA\u8c03\u7528\u4e86list.get\u65b9\u6cd5\uff0c\u8fd9\u65f6\u5c31\u4f1a\u53d1\u751f\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff0c\u5bfc\u81f4\u95ee\u9898\u7684\u539f\u56e0\u5c31\u662f\u4e0a\u9762\u7684\u590d\u5408\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u8fd9\u91cc\u53ef\u4ee5\u901a\u8fc7\u5728\u65b9\u6cd5\u5185\u90e8\u989d\u5916\u7684\u4f7f\u7528list\u5bf9\u8c61\u9501\u6765\u5b9e\u73b0\u539f\u5b50\u64cd\u4f5c\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u8fd8\u6ca1\u770b\u61c2\uff0c\u6211\u4eec\u518d\u7ee7\u7eed\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nif (!vector.contains(element)) \r\n\tvector.add(element); \r\n\t...\r\n\t\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n}<\/pre>\n<p>\n\t\u660e\u767d\u4e86\u5427\uff01\n<\/p>\n<p>\n\t\u8fd9\u662f\u7ecf\u5178\u7684 put-if-absent \u60c5\u51b5\uff0c\u5c3d\u7ba1 contains, add \u65b9\u6cd5\u90fd\u6b63\u786e\u5730\u540c\u6b65\u4e86\uff0c\u4f46\u4f5c\u4e3a vector \u4e4b\u5916\u7684\u4f7f\u7528\u73af\u5883\uff0c\u4ecd\u7136\u5b58\u5728 race condition\uff08\u9501\u7ade\u4e89\uff09\u3002\n<\/p>\n<p>\n\t\u867d\u7136\u6761\u4ef6\u5224\u65adcontains\u4e0eadd\u90fd\u662f\u539f\u5b50\u6027\u7684\u64cd\u4f5c (atomic)\uff0c\u4f46\u5728 if \u6761\u4ef6\u5224\u65ad\u4e3a\u771f\u540e\uff0c\u90a3\u4e2a\u7528\u6765\u8bbf\u95eevector.contains \u65b9\u6cd5\u7684\u9501\u5df2\u7ecf\u91ca\u653e\uff0c\u5728\u5373\u5c06\u7684 vector.add \u65b9\u6cd5\u8c03\u7528 \u4e4b\u95f4\u6709\u95f4\u9699\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u5b8c\u5168\u6709\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u83b7\u5f97 vector\u7684 lock \u5e76\u6539\u53d8\u5176\u72b6\u6001, \u6b64\u65f6\u5f53\u524d\u7ebf\u7a0b\u7684vector.add(element); \u6b63\u5728\u7b49\u5f85\uff08\u53ea\u4e0d\u8fc7\u6211\u4eec\u4e0d\u77e5\u9053\u800c\u5df2\uff09\u3002\u53ea\u6709\u5f53\u5176\u4ed6\u7ebf\u7a0b\u91ca\u653e\u4e86 vector \u7684 lock \u540e\uff0cvector.add(element); \u7ee7\u7eed\uff0c\u4f46\u6b64\u65f6\u5b83\u5df2\u7ecf\u57fa\u4e8e\u4e00\u4e2a\u9519\u8bef\u7684\u5047\u8bbe\u4e86\u3002\n<\/p>\n<p>\n\t\u5355\u4e2a\u7684\u65b9\u6cd5 synchronized \u4e86\u5e76\u4e0d\u4ee3\u8868\u7ec4\u5408\uff08compound\uff09\u7684\u65b9\u6cd5\u8c03\u7528\u5177\u6709\u539f\u5b50\u6027\uff0c\u4f7f compound actions \u6210\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u53ef\u80fd\u89e3\u51b3\u529e\u6cd5\u4e4b\u4e00\u8fd8\u662f\u79bb\u4e0d\u5f00intrinsic lock (\u8fd9\u4e2a\u9501\u5e94\u8be5\u662f vector \u7684\uff0c\u4f46\u7531 client \u7ef4\u62a4)\u3002\n<\/p>\n<p>\n\t\u6240\u4ee5\uff0c\u5bf9\u4e8e\u6587\u7ae0\u5f00\u5934\u7684\u90a3\u4e2a\u90a3\u4e2a&ldquo;\u611a\u8822&rdquo;\u7684\u95ee\u9898\uff0c\u6b63\u786e\u5730\u56de\u7b54\u5e94\u8be5\u662f\uff1a Vector \u548c ArrayList \u5b9e\u73b0\u4e86\u540c\u4e00\u63a5\u53e3 List, \u4f46\u6240\u6709\u7684 Vector \u7684\u65b9\u6cd5\u90fd\u5177\u6709 synchronized \u5173\u952e\u4fee\u9970\u3002\u4f46\u5bf9\u4e8e\u590d\u5408\u64cd\u4f5c\uff0cVector \u4ecd\u7136\u9700\u8981\u8fdb\u884c\u540c\u6b65\u5904\u7406\u3002\n<\/p>\n<p>\n\t\u4ece Java5 \u5f00\u59cb\uff0cJava.util.concurent \u5305\u4e0b\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u652f\u6301\u9ad8\u6548\u5e76\u53d1\u7684\u8bbf\u95ee\u7684\u96c6\u5408\u7c7b\u3002Vector \u5176\u5b9e\u5df2\u7ecf\u88abJDK\u629b\u5f03\u4e86\uff0c\u53ea\u662f\u4e3a\u4e86\u5411\u524d\u517c\u5bb9\uff0c\u8fd8\u7559\u7740\u5b83\u4eec\u3002Vector \u672c\u8eab\u6ca1\u6709\u89e3\u51b3\u591a\u7ebf\u7a0b\u95ee\u9898\uff0c\u53cd\u800c\uff0c\u5728\u5f15\u5165\u4e86 synchronized \u6982\u5ff5\u7684\u6df7\u4e71\u7684\u540c\u65f6\uff0c\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u4e3a synchronized \u7684\u5f00\u9500\u662f\u5de8\u5927\u7684\uff1a\u963b\u6b62\u7f16\u8bd1\u5668\u4e71\u5e8f\u3002\n<\/p>\n<p>\n\t\u6240\u4ee5\uff0c\u4ee5\u540e\u518d\u6709\u4eba\u7ed9\u4f60\u8bf4&nbsp;Vector \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u65f6\u5019\uff0c\u8bf7\u52a0\u6761\u4ef6\uff0c\u5426\u5219\u5c31\u6253\u4ed6\u7684\u8138\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3335\">Vector \u771f\u7684\u7ebf\u7a0b\u5b89\u5168\u7684\u5417\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4f60\u8fd8\u5728\u9762\u8bd5\u4e2d\u56de\u7b54&nbsp;Vector \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5417\uff1f\u6211\u76f8\u4fe1\u5f88\u591a\u4eba\u5728\u9762\u8bd5\u4e2d\u90fd\u662f\u8fd9\u6837\u56de\u7b54\u7684\uff0c\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[1741,426,1166,1098,531],"class_list":["post-3335","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-arraylist","tag-synchronized","tag-vector","tag-1098","tag-531"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3335"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3335\/revisions"}],"predecessor-version":[{"id":3336,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3335\/revisions\/3336"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}