Forums Register Login

can't understand about Synchronization

+Pie Number of slices to send: Send
Hello friends, I m new to java synchronization.
What is Synchronization ?
Synchronization is the process of locking a particular object to use shared resource. At a time only one thread can use the shared resource

I do know the flow of program ? can anyone help me to make understand this ?



[/b] OUTPUT [/b]
5
10
15
20
25
100
200
300
400
500

[/b] My doubt is[/b]

1.What is the flow of program ? and execution ?
2. Why constructors and this keyword is used here ?
3. How "t.printTable()" method comes when i type "t." in eclipse it automatically suggests then i clicked t.printTable (i do know how? i didn't create any Table object before calling "t.printTable" in both MyThread1 and MyThread2 Classes )

Please anyone kindly explain this program in briefly
1
+Pie Number of slices to send: Send
 

1.What is the flow of program ? and execution ?



Here, 2 things involves. First table which prints something after acquiring lock on itself and second is threads which calls this table to print something.

Table: Acquire lock on itself and print something.
MyThread1 & MyThread2: 2 threads which uses table references and then ask table to print.

As both the threads using same reference of the table so at a time only 1 thread will be executed.

Main thread: Create table instance. Create 2 threads and share same table instance with them. Then starts threads. When first thread start executing
it calls printsomething(..) and acquire lock on table and starts printing it. While at same time, 2nd thread also started and calls printsomething(..)
method, as 1st thread already acquire lock so it waits until first release the lock. Once first thread releases the lock, second acquires it and
start printing it.
1
+Pie Number of slices to send: Send
 

Why constructors and this keyword is used here ?


Constructors is used to create an object and initialilze initial values i.e. table reference.

this keyword represent own class object. so table class acquires lock on its class instance.

i didn't create any Table object before calling "t.printTable" in both MyThread1 and MyThread2 Classes )


you dint create any object but you shared the reference of table through constructor. Here below is your code:

+Pie Number of slices to send: Send
Thank you @Tushar It really helped me a lot I understand the flow of execution but i didn't understand the constructor part only



Here, we are creating instance variable "Table t" only know? then how it could be referenced for Table class ? can you please explain it again ? in different ways ?
+Pie Number of slices to send: Send
 

vinoth vino wrote:Hello friends, I m new to java synchronization.
What is Synchronization ?
Synchronization is the process of locking a particular object to use shared resource. At a time only one thread can use the shared resource


Yes, but the more important question is: Why do we need to do this?

I do know the flow of program


Do you? You know the flow when there's only one Thread, because it can only do one thing at a time; but suppose you launched five Threads to work on the same task - what happens then?

Answer; You can't predict the order in which things will be done. And the reason you can't is that Thread scheduling is not under your control; it's done by the JVM, or Operating system (or both), and it might start all 5 Threads, then give 10 minutes of processing time to the fifth Thread before it even looks at any of the others - highly unlikely, but you just don't know.

Furthermore, two (or more) Threads can access the same resource - field, method, object, file, URL etc - at exactly the same time. And if any of those things (usually a method) updates anything, its possible for another Thread to "see" the resource in an inconsistent state.

So, at it's foundation, all synchronization does is to stop that from happening - it says to the JVM:
"You see this updateAccountBalance() method? Well only ONE Thread can access it at any time; and when it's finished, ALL Threads will be able to see the the result of what it did."

That said, while it's definitely an important thing to know, it isn't required as much as you might think, and one of the common pitfalls when people are starting out is that they tend to over-synchronize - which can lead to slow execution and, in extreme cases, deadlock.

In many cases it can also be avoided completely. Take the String class: Quite a whopper in Java terms, in that it has 60-odd methods, yet it isn't synchronized at all.
Why? Because Strings are immutable - ie, you can't change them. And it's very often the best way to ensure that a class works in a multi-Threaded situation. Obviously it won't work for everything, but you'd be surprised how many places it does work.

And for small things like numbers and some basic collections, have a look at the java.util.concurrent and java.util.concurrent.atomic packages. They have some very neat classes (eg, AtomicInteger) that save you the rigmarole of having to write your own synchronisation, and in many cases work a lot faster than you could do by peppering "synchronized" throughout your code.

Hope it helps.

Winston
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 689 times.
Similar Threads
doubt on synchronized block-2
doubt on synchronized block-3
doubt on synchronized block in java
Synchronizing the Methods.
Multiple thread accessing List (ArrayList, Vector)
Question on Thread
synchronized threads
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:00:23.