Making everything synchronized except one method










0














Suppose that we have a large amount of previously-written code in which many parts may not be thread-safe. We add a method to the codes which is called somewhere at run-time which is very time-consuming but can be implemented thread-safe. Is there any way to make everything synchronized except the method, so that we can run multiple threads of the program and be sure that only the thread-safe method is run in parallel? If not, what can be done to run the method in parallel but everything else is run one-at-a-time? (Assume that due to the complexity of the code and the initializations required before the method is called we can not separate the run of the method and the rest of the code and the method is called somewhere in the middle of the run)










share|improve this question





















  • Can threads be running the thread-safe method at the same time some other thread is running one of the other methods? If not, then a ReadWriteLock is the answer, where the "thread-safe" method takes a read lock, and all the "synchronized" methods takes a write lock (instead of synchronizing).
    – Andreas
    Nov 12 at 20:48











  • But I can not modify all other methods (say 1000 methods) to take a lock.
    – Shayan
    Nov 12 at 21:00











  • 1000 methods? And all those methods are mutually exclusive with each other? That's code smell right there. Sounds like a God object.
    – Andreas
    Nov 12 at 21:48










  • Not necessarily in one class.
    – Shayan
    Nov 12 at 22:25















0














Suppose that we have a large amount of previously-written code in which many parts may not be thread-safe. We add a method to the codes which is called somewhere at run-time which is very time-consuming but can be implemented thread-safe. Is there any way to make everything synchronized except the method, so that we can run multiple threads of the program and be sure that only the thread-safe method is run in parallel? If not, what can be done to run the method in parallel but everything else is run one-at-a-time? (Assume that due to the complexity of the code and the initializations required before the method is called we can not separate the run of the method and the rest of the code and the method is called somewhere in the middle of the run)










share|improve this question





















  • Can threads be running the thread-safe method at the same time some other thread is running one of the other methods? If not, then a ReadWriteLock is the answer, where the "thread-safe" method takes a read lock, and all the "synchronized" methods takes a write lock (instead of synchronizing).
    – Andreas
    Nov 12 at 20:48











  • But I can not modify all other methods (say 1000 methods) to take a lock.
    – Shayan
    Nov 12 at 21:00











  • 1000 methods? And all those methods are mutually exclusive with each other? That's code smell right there. Sounds like a God object.
    – Andreas
    Nov 12 at 21:48










  • Not necessarily in one class.
    – Shayan
    Nov 12 at 22:25













0












0








0







Suppose that we have a large amount of previously-written code in which many parts may not be thread-safe. We add a method to the codes which is called somewhere at run-time which is very time-consuming but can be implemented thread-safe. Is there any way to make everything synchronized except the method, so that we can run multiple threads of the program and be sure that only the thread-safe method is run in parallel? If not, what can be done to run the method in parallel but everything else is run one-at-a-time? (Assume that due to the complexity of the code and the initializations required before the method is called we can not separate the run of the method and the rest of the code and the method is called somewhere in the middle of the run)










share|improve this question













Suppose that we have a large amount of previously-written code in which many parts may not be thread-safe. We add a method to the codes which is called somewhere at run-time which is very time-consuming but can be implemented thread-safe. Is there any way to make everything synchronized except the method, so that we can run multiple threads of the program and be sure that only the thread-safe method is run in parallel? If not, what can be done to run the method in parallel but everything else is run one-at-a-time? (Assume that due to the complexity of the code and the initializations required before the method is called we can not separate the run of the method and the rest of the code and the method is called somewhere in the middle of the run)







java multithreading synchronized






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 20:34









Shayan

1,19842547




1,19842547











  • Can threads be running the thread-safe method at the same time some other thread is running one of the other methods? If not, then a ReadWriteLock is the answer, where the "thread-safe" method takes a read lock, and all the "synchronized" methods takes a write lock (instead of synchronizing).
    – Andreas
    Nov 12 at 20:48











  • But I can not modify all other methods (say 1000 methods) to take a lock.
    – Shayan
    Nov 12 at 21:00











  • 1000 methods? And all those methods are mutually exclusive with each other? That's code smell right there. Sounds like a God object.
    – Andreas
    Nov 12 at 21:48










  • Not necessarily in one class.
    – Shayan
    Nov 12 at 22:25
















  • Can threads be running the thread-safe method at the same time some other thread is running one of the other methods? If not, then a ReadWriteLock is the answer, where the "thread-safe" method takes a read lock, and all the "synchronized" methods takes a write lock (instead of synchronizing).
    – Andreas
    Nov 12 at 20:48











  • But I can not modify all other methods (say 1000 methods) to take a lock.
    – Shayan
    Nov 12 at 21:00











  • 1000 methods? And all those methods are mutually exclusive with each other? That's code smell right there. Sounds like a God object.
    – Andreas
    Nov 12 at 21:48










  • Not necessarily in one class.
    – Shayan
    Nov 12 at 22:25















Can threads be running the thread-safe method at the same time some other thread is running one of the other methods? If not, then a ReadWriteLock is the answer, where the "thread-safe" method takes a read lock, and all the "synchronized" methods takes a write lock (instead of synchronizing).
– Andreas
Nov 12 at 20:48





Can threads be running the thread-safe method at the same time some other thread is running one of the other methods? If not, then a ReadWriteLock is the answer, where the "thread-safe" method takes a read lock, and all the "synchronized" methods takes a write lock (instead of synchronizing).
– Andreas
Nov 12 at 20:48













But I can not modify all other methods (say 1000 methods) to take a lock.
– Shayan
Nov 12 at 21:00





But I can not modify all other methods (say 1000 methods) to take a lock.
– Shayan
Nov 12 at 21:00













1000 methods? And all those methods are mutually exclusive with each other? That's code smell right there. Sounds like a God object.
– Andreas
Nov 12 at 21:48




1000 methods? And all those methods are mutually exclusive with each other? That's code smell right there. Sounds like a God object.
– Andreas
Nov 12 at 21:48












Not necessarily in one class.
– Shayan
Nov 12 at 22:25




Not necessarily in one class.
– Shayan
Nov 12 at 22:25












1 Answer
1






active

oldest

votes


















0














I assume the problem is that this 'thread-safe' method is called from synchronized methods, so it will run inside the synchronized block.



If this method has a return value or it alters any object which is used later inside the synchronized block then I think there is no way to solve this problem.



However if this method has no return value and any of its results are not need later in the current thread then you can implement it as an asynchronous method.



I have a similar solution in my current project: I have to log the events into an external DB. So my logEvent is an async method and although it's quite slow itself but at least it doesn't slow down the parent thread.






share|improve this answer




















  • Indeed, its effects are used in the rest of program.
    – Shayan
    Nov 12 at 21:01










  • Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
    – Selindek
    Nov 12 at 21:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269698%2fmaking-everything-synchronized-except-one-method%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I assume the problem is that this 'thread-safe' method is called from synchronized methods, so it will run inside the synchronized block.



If this method has a return value or it alters any object which is used later inside the synchronized block then I think there is no way to solve this problem.



However if this method has no return value and any of its results are not need later in the current thread then you can implement it as an asynchronous method.



I have a similar solution in my current project: I have to log the events into an external DB. So my logEvent is an async method and although it's quite slow itself but at least it doesn't slow down the parent thread.






share|improve this answer




















  • Indeed, its effects are used in the rest of program.
    – Shayan
    Nov 12 at 21:01










  • Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
    – Selindek
    Nov 12 at 21:11















0














I assume the problem is that this 'thread-safe' method is called from synchronized methods, so it will run inside the synchronized block.



If this method has a return value or it alters any object which is used later inside the synchronized block then I think there is no way to solve this problem.



However if this method has no return value and any of its results are not need later in the current thread then you can implement it as an asynchronous method.



I have a similar solution in my current project: I have to log the events into an external DB. So my logEvent is an async method and although it's quite slow itself but at least it doesn't slow down the parent thread.






share|improve this answer




















  • Indeed, its effects are used in the rest of program.
    – Shayan
    Nov 12 at 21:01










  • Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
    – Selindek
    Nov 12 at 21:11













0












0








0






I assume the problem is that this 'thread-safe' method is called from synchronized methods, so it will run inside the synchronized block.



If this method has a return value or it alters any object which is used later inside the synchronized block then I think there is no way to solve this problem.



However if this method has no return value and any of its results are not need later in the current thread then you can implement it as an asynchronous method.



I have a similar solution in my current project: I have to log the events into an external DB. So my logEvent is an async method and although it's quite slow itself but at least it doesn't slow down the parent thread.






share|improve this answer












I assume the problem is that this 'thread-safe' method is called from synchronized methods, so it will run inside the synchronized block.



If this method has a return value or it alters any object which is used later inside the synchronized block then I think there is no way to solve this problem.



However if this method has no return value and any of its results are not need later in the current thread then you can implement it as an asynchronous method.



I have a similar solution in my current project: I have to log the events into an external DB. So my logEvent is an async method and although it's quite slow itself but at least it doesn't slow down the parent thread.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 20:49









Selindek

998714




998714











  • Indeed, its effects are used in the rest of program.
    – Shayan
    Nov 12 at 21:01










  • Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
    – Selindek
    Nov 12 at 21:11
















  • Indeed, its effects are used in the rest of program.
    – Shayan
    Nov 12 at 21:01










  • Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
    – Selindek
    Nov 12 at 21:11















Indeed, its effects are used in the rest of program.
– Shayan
Nov 12 at 21:01




Indeed, its effects are used in the rest of program.
– Shayan
Nov 12 at 21:01












Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
– Selindek
Nov 12 at 21:11




Well, maybe it's still possible to do it, but it need a LOT of refactoring and it heavily depends on the other methods. (I think my original answer is still correct, because there is no easy way to achieve it) But you have to refactor the whole code by moving the unsafe parts to smaller synchronized blocks. - But that depends on the logic. There is no theoretical YES/NO answer.
– Selindek
Nov 12 at 21:11

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269698%2fmaking-everything-synchronized-except-one-method%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党