Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing instance of NumberFormat can cause diffrent results #227

Closed
capussopy opened this issue Jul 22, 2022 · 6 comments · Fixed by #238
Closed

Sharing instance of NumberFormat can cause diffrent results #227

capussopy opened this issue Jul 22, 2022 · 6 comments · Fixed by #238
Labels
bug Something isn't working

Comments

@capussopy
Copy link

When parallel requests are sent via filter, the sharing NumberFormat instance parse randomly different numbers. Sometimes it fails , sometime its correct and sometimes it parses a double out.

The issue is caused by your StringConverter.class

An simple example that causes the issue:

public class Scratch extends Thread {

    public static final NumberFormat NUMBER_FORMAT ;

    public static void main(String args[]) {
        for (int i = 0; i < 3; i++) {
            Scratch thread = new Scratch();
            thread.start();
        }
    }

    public void run() {
        ParsePosition position = new ParsePosition(0);
        Number number = NUMBER_FORMAT.parse("1011", position);
        System.out.println(number);
    }

    static {
        NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);
        NUMBER_FORMAT.setGroupingUsed(false);
    }
}

The issue disapers, if we use a new instance of Numberformat in every Thread:

public class Scratch extends Thread {


    public static void main(String args[]) {
        for (int i = 0; i < 5; i++) {
            Scratch thread = new Scratch();
            thread.start();
        }
    }

    public void run() {
        ParsePosition position = new ParsePosition(0);
        NumberFormat format = NumberFormat.getInstance(Locale.US);
        format.setGroupingUsed(false);
        Number number = format.parse("1011", position);
        System.out.println(number);
    }
  
}
@torshid torshid added the bug Something isn't working label Jul 25, 2022
@torshid
Copy link
Member

torshid commented Jul 25, 2022

@capussopy Thanks for noticing about this. I think that we have a similar issue with SimpleDateFormat, which is not thread safe too. It should be possible to wrap these fields with ThreadLocal, do you have other solutions?

@capussopy
Copy link
Author

@torshid This is true. The Javadoc for SimpleDateFormat says:

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

yes, you can use Threadlocal or create a new instance in each thread. I think the solution with ThreadLocal should have the better performance. But im not 100% sure.

@capussopy
Copy link
Author

@torshid any updates on this?

@FelipeBerrios
Copy link

any updates?

@torshid
Copy link
Member

torshid commented Nov 30, 2022

@FlorianCassayre
Copy link

@skamv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants