Question

I googled for AtomicInteger and I saw someone said we can use AtomicInteger(AtomicLong) for memory sequencer (http://www.cs.hut.fi/u/tlilja/multicore/slides/java_multicore.pdf). Here is my test:

public class TestAtomicInteger {

public static void main(String[] args) {

    final AtomicInteger sequencer = new AtomicInteger(1);
    final Set<Integer> integers = new HashSet<>();
    //final Set<Integer> integers = new ConcurrentSkipListSet<>();


    final Runnable task = new Runnable() {

        @Override
        public void run() {
            int next = sequencer.getAndIncrement();
            integers.add(next);
            System.out.println(integers.size());
        }
    };
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(task);
        t.start();
    }
}
}

After run this code for many times I found that sometime total of atomic output is not 1000. It means there is DUPLICATE return of getAndIncrement method.

Anyone explain why? Thanks.

*NOTE: In run function. If I use System.out.println(next); Sometime I see some missing sequencers too. *

SAMPLE OUTPUT 1:

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

Was it helpful?

Solution

One major issue with your code is that you add the numbers to a HashSet which is not thread safe. That is the reason for what you are observing. If you used a thread safe collection, you would get the expected result:

final Set<Integer> integers = new ConcurrentSkipListSet<>();

But that introduces an additional layer of synchronization. Another alternative is to use a plain array int[] numbers = new int[1000]; and use: numbers[next-1]++; to count the occurences.

New code for reference:

public static void main(String[] args) throws Exception {

    final AtomicInteger sequencer = new AtomicInteger(1);
    final int[] integers = new int[1000];

    final Runnable task = new Runnable() {

        @Override
        public void run() {
            int next = sequencer.getAndIncrement();
            integers[next-1]++;
        }
    };
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(task);
        t.start();
        threads.add(t);
    }
    for (Thread t : threads) {
        t.join();
    }
    for (int i = 0; i < 1000; i++) {
        if (integers[i] != 1) System.out.println(i + " -> " + integers[i]);
    }
}

OTHER TIPS

If I run your program on my computer, I observe incorrect output about 1 in 10 times. (This is with the console emulator in Netbeans 7.)

However, if I modify it to check the size of the set after all threads are complete, it always contains 1,000 unique integers. This is true even when there are errors in the output generated by the individual threads while it is running. (That is, in the lines printed by the threads, there are duplicates printed, but the true size of the set after execution is complete is actually 1,000)

Therefore I conclude that the problem is having 1,000 threads all writing to the PrintStream that your platform provides for System.out is corrupting the output. On my platform I can see corrupted output during execution, but there are always 1,000 unique integers in the set at the end.

public static void main(String[] args) throws InterruptedException {

    final AtomicInteger sequencer = new AtomicInteger(1);
    final Set<Integer> integers = new ConcurrentSkipListSet<Integer>();

    final Runnable task = new Runnable() {

        @Override
        public void run() {
            int next = sequencer.getAndIncrement();
            integers.add(next);
            System.out.println(next);
        }
    };
    List<Thread> threads = new ArrayList<Thread>(1000);
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(task);
        t.start();
        threads.add(t);
    }

    for (Thread t : threads) {
        t.join();
    }
    System.out.println(integers.size());
}

AtomicInteger makes calls to the Unsafe class, which in turn makes native calls. So yes AtomicInteger may theoretically return non atomic updates. This would depend on the JVM and and the underlying architecture it is running.

However given the nature of threads I would always assume the bug is in my code not the JVM.

I ran this code and was unable to detect any collisions. JDK 1_7_45 win 7

  public static void main(String[] args) {

      final AtomicInteger sequencer = new AtomicInteger(1);
      final Set<Integer> integers = new HashSet<Integer>();

      final Runnable task = new Runnable() {

          @Override
          public void run() {
              int next = sequencer.getAndIncrement();
              synchronized (integers){
                  if(integers.contains(next)){
                      System.out.println("duplicate detected " + next);
                  }
                  integers.add(next);
              }
          }
      };


      for(int j = 0; j < 1000; j++){
          System.out.print("testing " + j +" ");
          sequencer.set(0);
          integers.clear();
          List<Thread> threads = new ArrayList<Thread>(10000);
          for (int i = 0; i < 1000; i++) {
              Thread t = new Thread(task);
              threads.add(t);
              t.start();
          }
          for (Thread t : threads) {
              try {
                  t.join();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          System.out.println("integers size " + integers.size());
      }
  }

As others have said:

  1. Wait for all threads to finish at the end of the end of the loop using Thread.join(). References to all the Threads must have been stored in a Collection of course
  2. close() STDOUT before exiting

Around the program:

  1. Pipe the output through sort and/or wc -l
  2. Use diff to check against a list of numbers created by a simple loop
  3. Exit with warning if diff finds a difference

Repeat 10'000 times in a scripted loop.

Report back.

The updated output contains 7 ints printed twice.

(took your output | sort | uniq --count | sort )

prints this at the end (first col is number of times seen, 2nd col is the value)

  1 991
  1 992
  1 993
  2 519
  2 52
  2 661
  2 848
  2 875
  2 985
  2 995

This is because you are printing the size of the HashSet which is not threadsafe so sometimes the size isn't showing the updated value.

Changing to a threadsafe Set implementation should work.

To me, it's because of ConcurrentSkipListSet.size(). This kind of weakly consistent collection although thread-safe are not precise on iterations or counting in general.

From Javadoc of ConcurrentSkipListSet.size()

Beware that, unlike in most collections, the size method is not a constant-time operation. Because of the asynchronous nature of these sets, determining the current number of elements requires a traversal of the elements. Additionally, the bulk operations addAll, removeAll, retainAll, and containsAll are not guaranteed to be performed atomically. For example, an iterator operating concurrently with an addAll operation might view only some of the added elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top