Many articles on internet specifies that using String.intern()
in multi threading is bad but I really could not understand why it is bad.Using String.intern()
always return a unique string from string pool,isn't it?
If that is not the case then Is JVM string pool thread local? If not then why using String.intern()
for synchronizing in multithreaded environment is considered bad? So in the following use case, wouldn't it solve the synchronization issue:
Method1 {
synchronized(Interned string) {
select method {
select query to databse
}
...some processing...
update method {
update query to database
}
}
}
Method2 {
synchronized(Interned string) {
select method {
select query to databse
}
.....some processing....
insert method {
insert query to database
}
}
}
Here I am synchronizing both the methods based on one common string id.I want to execute entire method as one transaction(preventing other method to even read the database).But doing this at database level is causing deadlock(not preventing read access). Is synchronizing using string intern in this case has any bottleneck or deadlock issue? Is there any other approach to solve the issue? forgive me for any inconvenience or bad formatting.