利用加了写锁后,将阻塞后续尝试加写锁的线程的特性,为写入用户历史动作增加写锁,以达到在写过程中不受其他线程影响的效果,来保证每个用户只能领取一次金额。参考代码如下:
package com.miqtech.test.readwritelock; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; import org.junit.Test; public class ReadWriteLockRedbagTest { private static List<SRHistory> history = new ArrayList<SRHistory>(); private static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private static WriteLock wl = rwl.writeLock(); @Test public void test() { ExecutorService pool = Executors.newFixedThreadPool(10); int maxUserId = 1000; for (int i = 0; i < maxUserId; i++) { pool.execute(new Executer(i)); Random r = new Random(); pool.execute(new Executer(r.nextInt(maxUserId))); } pool.shutdown(); // 等待1秒,确保1000次操作执行完成后,再读取历史记录 try { Thread.sleep(1000); } catch (InterruptedException e) { } readHistory(); } /** * 打开红包,获取金额 */ private void getAmount(int userId) { SRHistory h = new SRHistory(); h.setUserId(userId); Random r = new Random(); int amount = r.nextInt(9) + 1; h.setAmount(amount); addHistory(h); } /** * 查询用户的历史记录 */ private SRHistory getHistoryByUserId(int userId) { SRHistory result = null; for (SRHistory h : history) { if (h.getUserId().equals(userId)) { result = h; } } return result; } /** * 写入用户的红包记录 */ private void addHistory(SRHistory h) { wl.lock(); if (getHistoryByUserId(h.getUserId()) == null) { history.add(h); } wl.unlock(); } /** * 查看总体历史记录 */ private void readHistory() { Collections.sort(history); for (SRHistory h : history) { System.out.println(h.getUserId() + " -> " + h.getAmount()); } } /** * 获取红包行为的执行者 */ class Executer implements Runnable { private int userId; public Executer(int userId) { this.userId = userId; } @Override public void run() { getAmount(userId); } } } /** * 用户红包信息 */ class SRHistory implements Comparable<SRHistory> { private Integer userId; private Integer amount; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } @Override public int compareTo(SRHistory b) { if (this.getUserId().equals(b.getUserId())) { return 0; } return this.getUserId() > b.getUserId() ? 1 : -1; } }
执行结果如下:
0 -> 7 1 -> 9 2 -> 5 3 -> 1 4 -> 3 5 -> 9 6 -> 8 7 -> 5 8 -> 2 9 -> 3 10 -> 4 11 -> 1 12 -> 4 13 -> 3 14 -> 5 15 -> 1 16 -> 1 17 -> 6 18 -> 5 19 -> 8 20 -> 7 21 -> 1 22 -> 8 23 -> 8 24 -> 2 25 -> 9 26 -> 6 27 -> 6 28 -> 6 29 -> 7 30 -> 8 31 -> 2 32 -> 9 33 -> 3 34 -> 1 35 -> 8 36 -> 5 37 -> 5 38 -> 6 39 -> 8 40 -> 4 41 -> 5 42 -> 3 43 -> 8 44 -> 2 45 -> 9 46 -> 8 47 -> 5 48 -> 5 49 -> 5 50 -> 8 51 -> 5 52 -> 8 53 -> 5 54 -> 5 55 -> 9 56 -> 4 57 -> 5 58 -> 4 59 -> 1 60 -> 7 61 -> 1 62 -> 1 63 -> 7 64 -> 3 65 -> 2 66 -> 9 67 -> 9 68 -> 6 69 -> 2 70 -> 2 71 -> 3 72 -> 2 73 -> 1 74 -> 4 75 -> 8 76 -> 9 77 -> 4 78 -> 2 79 -> 9 80 -> 6 81 -> 4 82 -> 2 83 -> 3 84 -> 6 85 -> 6 86 -> 4 87 -> 9 88 -> 2 89 -> 3 90 -> 2 91 -> 7 92 -> 9 93 -> 6 94 -> 9 95 -> 7 96 -> 7 97 -> 6 98 -> 1 99 -> 5 100 -> 8 101 -> 6 102 -> 3 103 -> 1 104 -> 9 105 -> 6 106 -> 7 107 -> 4 108 -> 1 109 -> 5 110 -> 6 111 -> 5 112 -> 3 113 -> 3 114 -> 2 115 -> 7 116 -> 1 117 -> 9 118 -> 8 119 -> 4 120 -> 3 121 -> 5 122 -> 8 123 -> 2 124 -> 3 125 -> 4 126 -> 6 127 -> 6 128 -> 8 129 -> 6 130 -> 1 131 -> 2 132 -> 6 133 -> 2 134 -> 9 135 -> 5 136 -> 1 137 -> 4 138 -> 8 139 -> 1 140 -> 4 141 -> 2 142 -> 7 143 -> 4 144 -> 3 145 -> 1 146 -> 4 147 -> 7 148 -> 3 149 -> 5 150 -> 8 151 -> 3 152 -> 4 153 -> 7 154 -> 3 155 -> 3 156 -> 9 157 -> 8 158 -> 6 159 -> 9 160 -> 2 161 -> 8 162 -> 8 163 -> 3 164 -> 7 165 -> 7 166 -> 8 167 -> 2 168 -> 7 169 -> 5 170 -> 8 171 -> 1 172 -> 7 173 -> 9 174 -> 4 175 -> 5 176 -> 2 177 -> 6 178 -> 8 179 -> 1 180 -> 7 181 -> 7 182 -> 6 183 -> 5 184 -> 6 185 -> 8 186 -> 7 187 -> 2 188 -> 5 189 -> 4 190 -> 5 191 -> 3 192 -> 5 193 -> 2 194 -> 3 195 -> 1 196 -> 7 197 -> 5 198 -> 7 199 -> 5 200 -> 2 201 -> 7 202 -> 9 203 -> 4 204 -> 5 205 -> 9 206 -> 4 207 -> 2 208 -> 7 209 -> 5 210 -> 6 211 -> 4 212 -> 3 213 -> 9 214 -> 5 215 -> 3 216 -> 9 217 -> 4 218 -> 2 219 -> 9 220 -> 3 221 -> 1 222 -> 9 223 -> 8 224 -> 9 225 -> 2 226 -> 9 227 -> 6 228 -> 2 229 -> 8 230 -> 8 231 -> 1 232 -> 5 233 -> 6 234 -> 8 235 -> 4 236 -> 6 237 -> 5 238 -> 3 239 -> 7 240 -> 1 241 -> 8 242 -> 1 243 -> 4 244 -> 1 245 -> 2 246 -> 9 247 -> 4 248 -> 3 249 -> 1 250 -> 8 251 -> 9 252 -> 3 253 -> 2 254 -> 4 255 -> 4 256 -> 5 257 -> 5 258 -> 4 259 -> 4 260 -> 7 261 -> 6 262 -> 8 263 -> 8 264 -> 5 265 -> 9 266 -> 3 267 -> 3 268 -> 3 269 -> 5 270 -> 9 271 -> 2 272 -> 9 273 -> 3 274 -> 5 275 -> 3 276 -> 1 277 -> 5 278 -> 2 279 -> 2 280 -> 5 281 -> 2 282 -> 2 283 -> 9 284 -> 6 285 -> 1 286 -> 4 287 -> 5 288 -> 4 289 -> 4 290 -> 7 291 -> 8 292 -> 3 293 -> 5 294 -> 6 295 -> 2 296 -> 8 297 -> 1 298 -> 5 299 -> 6 300 -> 9 301 -> 8 302 -> 7 303 -> 1 304 -> 9 305 -> 4 306 -> 7 307 -> 6 308 -> 9 309 -> 6 310 -> 7 311 -> 4 312 -> 3 313 -> 6 314 -> 8 315 -> 6 316 -> 2 317 -> 3 318 -> 1 319 -> 1 320 -> 8 321 -> 7 322 -> 8 323 -> 6 324 -> 9 325 -> 9 326 -> 6 327 -> 4 328 -> 8 329 -> 9 330 -> 5 331 -> 1 332 -> 4 333 -> 5 334 -> 7 335 -> 5 336 -> 1 337 -> 9 338 -> 2 339 -> 9 340 -> 6 341 -> 9 342 -> 6 343 -> 2 344 -> 6 345 -> 5 346 -> 9 347 -> 5 348 -> 8 349 -> 3 350 -> 8 351 -> 2 352 -> 1 353 -> 9 354 -> 8 355 -> 6 356 -> 9 357 -> 4 358 -> 3 359 -> 7 360 -> 5 361 -> 1 362 -> 2 363 -> 1 364 -> 1 365 -> 2 366 -> 8 367 -> 1 368 -> 8 369 -> 6 370 -> 6 371 -> 4 372 -> 2 373 -> 8 374 -> 4 375 -> 8 376 -> 9 377 -> 1 378 -> 5 379 -> 6 380 -> 2 381 -> 7 382 -> 5 383 -> 3 384 -> 8 385 -> 3 386 -> 5 387 -> 2 388 -> 6 389 -> 8 390 -> 2 391 -> 8 392 -> 7 393 -> 8 394 -> 2 395 -> 4 396 -> 4 397 -> 6 398 -> 2 399 -> 4 400 -> 8 401 -> 1 402 -> 3 403 -> 7 404 -> 7 405 -> 2 406 -> 2 407 -> 2 408 -> 7 409 -> 2 410 -> 1 411 -> 3 412 -> 7 413 -> 4 414 -> 6 415 -> 1 416 -> 8 417 -> 7 418 -> 8 419 -> 5 420 -> 6 421 -> 2 422 -> 2 423 -> 5 424 -> 3 425 -> 5 426 -> 7 427 -> 2 428 -> 9 429 -> 4 430 -> 7 431 -> 9 432 -> 5 433 -> 7 434 -> 9 435 -> 2 436 -> 3 437 -> 4 438 -> 2 439 -> 9 440 -> 2 441 -> 8 442 -> 8 443 -> 2 444 -> 8 445 -> 9 446 -> 8 447 -> 7 448 -> 3 449 -> 2 450 -> 4 451 -> 6 452 -> 9 453 -> 8 454 -> 5 455 -> 5 456 -> 1 457 -> 3 458 -> 6 459 -> 6 460 -> 6 461 -> 5 462 -> 1 463 -> 4 464 -> 2 465 -> 6 466 -> 8 467 -> 8 468 -> 5 469 -> 2 470 -> 2 471 -> 5 472 -> 5 473 -> 9 474 -> 9 475 -> 2 476 -> 8 477 -> 4 478 -> 9 479 -> 3 480 -> 7 481 -> 2 482 -> 7 483 -> 2 484 -> 6 485 -> 1 486 -> 2 487 -> 6 488 -> 6 489 -> 9 490 -> 3 491 -> 9 492 -> 7 493 -> 9 494 -> 4 495 -> 4 496 -> 9 497 -> 1 498 -> 3 499 -> 1 500 -> 4 501 -> 1 502 -> 6 503 -> 3 504 -> 9 505 -> 6 506 -> 8 507 -> 5 508 -> 4 509 -> 3 510 -> 7 511 -> 4 512 -> 7 513 -> 3 514 -> 6 515 -> 4 516 -> 2 517 -> 8 518 -> 4 519 -> 9 520 -> 4 521 -> 1 522 -> 3 523 -> 4 524 -> 7 525 -> 1 526 -> 8 527 -> 5 528 -> 2 529 -> 5 530 -> 3 531 -> 6 532 -> 6 533 -> 4 534 -> 3 535 -> 2 536 -> 9 537 -> 6 538 -> 5 539 -> 2 540 -> 1 541 -> 2 542 -> 1 543 -> 8 544 -> 1 545 -> 9 546 -> 9 547 -> 8 548 -> 4 549 -> 2 550 -> 8 551 -> 1 552 -> 9 553 -> 9 554 -> 9 555 -> 1 556 -> 2 557 -> 8 558 -> 5 559 -> 5 560 -> 2 561 -> 1 562 -> 2 563 -> 8 564 -> 9 565 -> 6 566 -> 9 567 -> 3 568 -> 4 569 -> 2 570 -> 3 571 -> 2 572 -> 9 573 -> 6 574 -> 5 575 -> 5 576 -> 5 577 -> 4 578 -> 3 579 -> 8 580 -> 6 581 -> 4 582 -> 7 583 -> 5 584 -> 3 585 -> 4 586 -> 2 587 -> 2 588 -> 8 589 -> 7 590 -> 9 591 -> 4 592 -> 7 593 -> 2 594 -> 5 595 -> 1 596 -> 3 597 -> 7 598 -> 8 599 -> 5 600 -> 6 601 -> 4 602 -> 3 603 -> 2 604 -> 2 605 -> 2 606 -> 4 607 -> 6 608 -> 2 609 -> 3 610 -> 2 611 -> 5 612 -> 4 613 -> 1 614 -> 4 615 -> 2 616 -> 2 617 -> 8 618 -> 1 619 -> 2 620 -> 9 621 -> 8 622 -> 3 623 -> 6 624 -> 6 625 -> 8 626 -> 9 627 -> 7 628 -> 5 629 -> 7 630 -> 4 631 -> 5 632 -> 1 633 -> 1 634 -> 6 635 -> 9 636 -> 3 637 -> 1 638 -> 7 639 -> 9 640 -> 9 641 -> 8 642 -> 9 643 -> 4 644 -> 8 645 -> 9 646 -> 1 647 -> 5 648 -> 3 649 -> 1 650 -> 7 651 -> 7 652 -> 7 653 -> 1 654 -> 8 655 -> 3 656 -> 9 657 -> 4 658 -> 8 659 -> 2 660 -> 9 661 -> 6 662 -> 3 663 -> 9 664 -> 9 665 -> 5 666 -> 3 667 -> 2 668 -> 2 669 -> 6 670 -> 5 671 -> 5 672 -> 2 673 -> 5 674 -> 4 675 -> 7 676 -> 1 677 -> 7 678 -> 4 679 -> 1 680 -> 6 681 -> 5 682 -> 2 683 -> 8 684 -> 8 685 -> 2 686 -> 8 687 -> 2 688 -> 3 689 -> 7 690 -> 1 691 -> 2 692 -> 2 693 -> 2 694 -> 8 695 -> 1 696 -> 7 697 -> 7 698 -> 7 699 -> 6 700 -> 8 701 -> 1 702 -> 9 703 -> 2 704 -> 7 705 -> 8 706 -> 3 707 -> 6 708 -> 1 709 -> 9 710 -> 5 711 -> 6 712 -> 6 713 -> 2 714 -> 1 715 -> 2 716 -> 9 717 -> 5 718 -> 4 719 -> 6 720 -> 3 721 -> 8 722 -> 4 723 -> 5 724 -> 4 725 -> 6 726 -> 4 727 -> 4 728 -> 8 729 -> 6 730 -> 9 731 -> 7 732 -> 6 733 -> 4 734 -> 7 735 -> 3 736 -> 3 737 -> 8 738 -> 6 739 -> 9 740 -> 4 741 -> 6 742 -> 4 743 -> 4 744 -> 8 745 -> 6 746 -> 9 747 -> 2 748 -> 7 749 -> 9 750 -> 3 751 -> 3 752 -> 5 753 -> 3 754 -> 4 755 -> 9 756 -> 8 757 -> 6 758 -> 8 759 -> 1 760 -> 7 761 -> 8 762 -> 2 763 -> 4 764 -> 6 765 -> 4 766 -> 9 767 -> 2 768 -> 1 769 -> 3 770 -> 9 771 -> 9 772 -> 4 773 -> 9 774 -> 4 775 -> 9 776 -> 6 777 -> 6 778 -> 4 779 -> 9 780 -> 9 781 -> 1 782 -> 4 783 -> 7 784 -> 9 785 -> 8 786 -> 1 787 -> 2 788 -> 8 789 -> 3 790 -> 4 791 -> 7 792 -> 9 793 -> 8 794 -> 4 795 -> 6 796 -> 5 797 -> 5 798 -> 7 799 -> 3 800 -> 9 801 -> 3 802 -> 3 803 -> 9 804 -> 7 805 -> 1 806 -> 5 807 -> 5 808 -> 7 809 -> 5 810 -> 6 811 -> 5 812 -> 5 813 -> 8 814 -> 9 815 -> 9 816 -> 7 817 -> 3 818 -> 5 819 -> 7 820 -> 7 821 -> 4 822 -> 6 823 -> 5 824 -> 8 825 -> 1 826 -> 8 827 -> 5 828 -> 9 829 -> 5 830 -> 2 831 -> 1 832 -> 2 833 -> 3 834 -> 3 835 -> 6 836 -> 5 837 -> 6 838 -> 8 839 -> 1 840 -> 4 841 -> 1 842 -> 8 843 -> 5 844 -> 1 845 -> 8 846 -> 3 847 -> 1 848 -> 3 849 -> 1 850 -> 3 851 -> 2 852 -> 7 853 -> 6 854 -> 7 855 -> 7 856 -> 1 857 -> 7 858 -> 1 859 -> 5 860 -> 2 861 -> 5 862 -> 9 863 -> 3 864 -> 8 865 -> 1 866 -> 6 867 -> 8 868 -> 1 869 -> 3 870 -> 2 871 -> 2 872 -> 8 873 -> 2 874 -> 2 875 -> 7 876 -> 2 877 -> 2 878 -> 8 879 -> 8 880 -> 9 881 -> 9 882 -> 8 883 -> 2 884 -> 7 885 -> 4 886 -> 2 887 -> 9 888 -> 6 889 -> 7 890 -> 9 891 -> 7 892 -> 2 893 -> 9 894 -> 1 895 -> 4 896 -> 5 897 -> 2 898 -> 9 899 -> 6 900 -> 1 901 -> 9 902 -> 3 903 -> 5 904 -> 7 905 -> 7 906 -> 1 907 -> 9 908 -> 4 909 -> 7 910 -> 3 911 -> 4 912 -> 6 913 -> 1 914 -> 1 915 -> 8 916 -> 6 917 -> 8 918 -> 4 919 -> 8 920 -> 8 921 -> 2 922 -> 1 923 -> 1 924 -> 1 925 -> 9 926 -> 2 927 -> 5 928 -> 9 929 -> 5 930 -> 3 931 -> 4 932 -> 5 933 -> 1 934 -> 8 935 -> 4 936 -> 8 937 -> 6 938 -> 8 939 -> 9 940 -> 2 941 -> 6 942 -> 9 943 -> 2 944 -> 6 945 -> 9 946 -> 5 947 -> 2 948 -> 2 949 -> 5 950 -> 8 951 -> 4 952 -> 3 953 -> 3 954 -> 7 955 -> 9 956 -> 5 957 -> 7 958 -> 4 959 -> 3 960 -> 3 961 -> 7 962 -> 7 963 -> 4 964 -> 3 965 -> 9 966 -> 8 967 -> 7 968 -> 5 969 -> 8 970 -> 1 971 -> 8 972 -> 4 973 -> 3 974 -> 8 975 -> 7 976 -> 8 977 -> 5 978 -> 1 979 -> 8 980 -> 6 981 -> 2 982 -> 9 983 -> 7 984 -> 2 985 -> 8 986 -> 8 987 -> 1 988 -> 1 989 -> 5 990 -> 3 991 -> 3 992 -> 2 993 -> 9 994 -> 8 995 -> 2 996 -> 3 997 -> 8 998 -> 6 999 -> 1 Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
时间: 2024-11-02 09:30:55