Skip to content

FieldUtils may have concurrency issues #51

@matebase

Description

@matebase

for example: String id=FieldUtils.getFieldValue(next, "id")

public class Base implements Serializable {
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}
public class Son extends Base implements Serializable {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public class TestCase {

    public static void main(String[] args) {

        List<Son> records = new ArrayList<>();
        List<Son> records2 = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
            long l = random.nextLong();
            Son son = new Son();
            son.setId(l + "");
            records.add(son);
            Son son2 = new Son();
            son2.setId(l + "");
            records2.add(son2);
        }
        new Thread(() -> {
            Iterator<Son> iterator = records.iterator();
            while (iterator.hasNext()) {
                Son next = iterator.next();
                String id = FieldUtils.getFieldValue(next, "id");
            }
        }).start();

        new Thread(() -> {
            Iterator<Son> iterator = records2.iterator();
            while (iterator.hasNext()) {
                Son next = iterator.next();
                String id = FieldUtils.getFieldValue(next, "id");
            }
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Exception in thread "Thread-1" java.lang.IllegalStateException: Could not access method or field: Class org.springframework.util.ReflectionUtils can not access a member of class com.Base with modifiers "private"
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:107)
at org.springframework.util.ReflectionUtils.getField(ReflectionUtils.java:652)
at com.alibaba.spring.util.FieldUtils.getFieldValue(FieldUtils.java:72)
at com.alibaba.spring.util.FieldUtils.getFieldValue(FieldUtils.java:28)
at com.TestCase.lambda$main$1(TestCase.java:41)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions