In the Multithreading concept, multiple threads attempt to access the same resource simultaneously, which leads to inconsistent results. Public static void main() throws Ģ: invokestatic #5 // Method java/util/concurrent/Executors.newScheduledThreadPool:(I)Ljava/util/concurrent/ScheduledExecutorService Ģ4: invokedynamic #8, 0 // InvokeDynamic #0:run:(LmB2/ch7/PQ38 )Ljava/lang/Runnable Ģ9: invokeinterface #9, 2 // InterfaceMethod java/util/concurrent/ExecutorService.submit:(Ljava/lang/Runnable )Ljava/util/concurrent/Future ģ7: invokedynamic #10, 0 // InvokeDynamic #1:run:(LmB2/ch7/PQ38 )Ljava/lang/Runnable Ĥ2: invokeinterface #9, 2 // InterfaceMethod java/util/concurrent/ExecutorService.submit:(Ljava/lang/Runnable )Ljava/util/concurrent/Future ĥ5: invokeinterface #11, 1 // InterfaceMethod java/util/concurrent/ExecutorService.shutdown:()VĦ4: getstatic #14 // Field java/util/concurrent/TimeUnit.SECONDS:Ljava/util/concurrent/TimeUnit Ħ7: invokeinterface #15, 4 // InterfaceMethod java/util/concurrent/ExecutorService.awaitTermination:(JLjava/util/concurrent/TimeUnit )Zħ3: getstatic #16 // Field java/lang/System.out:Ljava/io/PrintStream ħ8: getstatic #2 // Field cookies:Ljava/lang/Integer Ĩ1: invokevirtual #17 // Method java/io/PrintStream.print:(Ljava/lang/Object )VĢ: invokestatic #4 // Method java/lang/Integer.Java's synchronization feature controls reference to shared resources by multiple threads. I am not good in bytecode may be it help you in finding the solutionġ: invokespecial #1 // Method java/lang/Object."":()VĠ: getstatic #2 // Field cookies:Ljava/lang/Integer Ħ: getstatic #2 // Field cookies:Ljava/lang/Integer ĩ: invokevirtual #3 // Method java/lang/Integer.intValue:()Iġ4: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer ġ7: putstatic #2 // Field cookies:Ljava/lang/Integer You are right += and -= are not atomic operations but i am using under synchronized so it must be atomic. My question is why it predicate different result since we are using synchronized keyword ? So it lock the cookies variable by using monitor of java bytecode. I may be better to say “monitor” than ”lock”.Īctually, I had modified from the book Java® SE 8 Programmer practice test Scott Selikoff, Jeanne Boyarsky Chapter 20 Java Concurrency Q38. So, if thread₂₈ happens to execute both lines 24 and 25, there is a risk that the withdraw() method will run before the “new” value from deposit() is visible, so you can expect to get “wrong” values occasionally, and your 9999 iterations mean it is likelier than not that such a race condition will occur at some point giving rise to a wrong result. (I don't think they are atomic operations on an int, but I am not certain.) But if they are executed on the same thread, it is possible for the withdraw() method to start before deposit() has completed since += and -= are not atomic operations on an Integer. If they are different, it will be impossible for line 25 to be executed until after line 24 and the associated method call have completed. I am not sure, but I think you will get problems if the same thread is executed in line 25 and in line 24. Result were : -5, 0, 5, 10When I tried your code on JShell, I got various results, but none of them positive. But if you only have one instance of PQ38, you aren't going to notice the difference. Static Integer cookies = 0 That is very different from what you posted yesterday. Is that the original question, or what you modified? What else did the question say there is always more than the code. Which book? Please always give full details of sources for all such material, at least authors and book title. Sorry for delay the site has been “down”.ĭhananjaykumar Kushwaha wrote. ![]() If you reassign amount from either method, who knows what will happen. That would happen if the two methods call each other. ![]() Only the same thread can go from bar() to foo() or vice versa. public void foo() Now whichever thread executes either of those methods will set and claim ownership of the lock on this it is only possible for one thread to execute those methods at once. You won't notice any difference until you have other synchronised blocks. It also provides some memory barrier effects for consistency and predictability of the values of shared variables, but that part isn't pertinent to your question. ¹ Actually that's all it does in terms of mutual exclusion. When we do synchronized (this) that just means we're obtaining the lock for the "current" object. ![]() The only thing that the synchronized keyword does¹ is to obtain the lock for the indicated object, blocking at that point in its execution until the lock is released if some other thread currently hodls it. It's telling us which lock we're going to obtain. ![]() Krishna Chhabra wrote:Not able to understand exactly that what does (this) means in below code
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |