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

Unsafe increment of static field #1574

Open
sebbASF opened this issue Dec 5, 2023 · 2 comments
Open

Unsafe increment of static field #1574

sebbASF opened this issue Dec 5, 2023 · 2 comments

Comments

@sebbASF
Copy link

sebbASF commented Dec 5, 2023

Mutable static fields should be synchronised to ensure safe publication.
There is no guarantee that the following code will generate unique names:

private static long messageLoopId = 0;
public MessageLoopThread() {
setName("JNA User32 MessageLoop " + (++messageLoopId));
}

The messageLoopId variable should probably be replaced by an AtomicLong.

@sebbASF
Copy link
Author

sebbASF commented Dec 5, 2023

Same issue here:

Also, this is an int, whereas the other is a long, but both are used for thread ids. Is it certain that an int is sufficient here?

@matthiasblaesing
Copy link
Member

I don't see a real problem. These are the names of the threads, so it might not look nice in the end, but will also not cause harm. What is more, if you really spawn threads at a rate where this becomes problematic, you have other problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants