ToStringBuilder and multi-thread environment

Just found some article about using Apache Commons' ToStringBuilder tool and about what a wonderful tool it is. And I decided to write about it too.

Well, there were few projects where I was able to use Apache Commons Lang tool called ToStringBuilder. It's very useful tool that helps to print information about class instance in the toString() method. I just was need to write

@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("Id", id)
.append("Name", name)
.append("Description", description)
.toString();
}

and as result got a string with nice-look instance description. It was perfect for me.
But now I'm not using this tool. And wasn't using it for the last 10 months as well. I would be happy to use it rather than own implementation over StringBuilder but I can't.
ToStringBuilder has problems with running in multi-thread environment. If you have class A that uses ToStringBuilder and you're going to use instances of this class in different threads - be careful!

2 comments:

Neox said...

Руслан, а ти пишеш якісь проекти зараз?

Ruslan Khmelyuk said...

Привіт, так звичайно пишу :)
Сонце, спека - а я все пишу, пишу, пишу..