There is a problem with the value of hashset set method. In the running result, there is only one element in the result set, and the value obtained by, size () is > 1.

1, the following code

package gof.singleton;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

//
public class Singleton2 {
    
    private static Singleton2 singleton = new Singleton2();
    
    private Singleton2() {}
    
    public static Singleton2 getSingleton() {
        return singleton;
    }
    
    public static void main(String[] args) throws InterruptedException {
        
        for(int j = 0;j<10;jPP) {
            CountDownLatch c = new CountDownLatch(1000);
            Set<Singleton2> list = new HashSet<Singleton2>();
            for(int i= 0 ;i<1000;iPP) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        list.add(Singleton2.getSingleton());
                        c.countDown();
                    }
                }).start();
            }
            c.await();
            System.out.println(list + "-" + list.size());
            //list.stream().forEach(System.out::println);
        }
        
        
        
    }
}

2, a possible result

[gof.singleton.Singleton2@a627065]-3
[gof.singleton.Singleton2@a627065]-5
[gof.singleton.Singleton2@a627065]-2
[gof.singleton.Singleton2@a627065]-5
[gof.singleton.Singleton2@a627065]-2
[gof.singleton.Singleton2@a627065]-1
[gof.singleton.Singleton2@a627065]-4
[gof.singleton.Singleton2@a627065]-3
[gof.singleton.Singleton2@a627065]-1
[gof.singleton.Singleton2@a627065]-3

3, question
Why the number of elements in the collection does not match the number printed-sharp-sharp-sharp topic description

sources of topics and their own ideas

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

May.10,2021

HashSet uses the add method to call the put method of HashMap. List.size () is the size () method of HashMap, which returns the size property of HashMap. The size of
list is greater than 1 because HashMap concurrency causes thread safety problems.
replace it with Set < Singleton2 > list = Collections.synchronizedSet (new HashSet < Singleton2 > ());


Java the documentation on HashSet clearly indicates that this class is not thread safe, so never manipulate the same HashSet object with multiple threads.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b367ff-2b567.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b367ff-2b567.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?