









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In all programming language only syntax is different not the logic. This course discuss core concepts for many different programming language and techniques. Key points for this lecture are: Java Concurrency, Parallel Processes, Concurrent Processes, Asynchronous, Processors, Threads, Mutable and Immutable Objects, Synchronized Statement, Atomic Actions, Local Variables
Typology: Slides
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Supply a public void run() method Create an object o of that class Tell the object to start: o.start();
Supply a public void run() method Create an object o of that class Create a Thread that “knows” o: Thread t = new Thread(o); Tell the Thread to start: t.start();
Sun provides a number of immutable objects You can create an ad hoc immutable object by simply not providing any way to change it All fields must be final (private may not be enough) No methods may change any of the object’s data You must ensure no access to the object until after it is completely constructed
Don’t try to find clever reasons to think you can avoid synchronization
synchronized public void myMethod( /* arguments /) { / some statements */ }
public void myMethod( /* arguments /) { synchronized(this) { / some statements */ } }
They are synchronized on the class object (a built-in object that represents the class)
A Vector is like an ArrayList, but is synchronized
Hence, the following code looks reasonable:
if (!myVector.contains(someObject)) { // check myVector.add(someObject); // act }
But there is a “gap” between checking the Vector and adding to it
During this gap, some other Thread may have added the object to the array Check-then-act code, as in this example, is unsafe
You must ensure that no other Thread executes during the gap
synchronized(myVector) { if (!myVector.contains(someObject)) { myVector.add(someObject); } }
So, what good is it that Vector is synchronized?
It means that each call to a Vector operation is atomic
Synchronization can be done on any object
Synchronization is on objects, not on variables
Suppose you have synchronized(myVector) { … }
Then it is okay to modify myVector—that is, change the values of its fields
It is not okay to say myVector = new Vector();
Synchronization is expensive
Synchronization entails a certain amount of overhead Synchronization limits parallelism (obviously, since it keeps other Threads from executing)
Moral: Don’t synchronize everything!
When all non-daemon Threads die, the daemon Threads are automatically terminated If the main Thread quits, the program will appear to quit, but other non- daemon Threads may continue to run These Threads will persist until you reboot your computer
synchronized(someObj) { flag = !flag; } synchronized(someObj) { if (flag) doSomething(); }
These methods were not safe and were deprecated almost immediately— never use them! They are still there because Java never throws anything away If you want one Thread to control another Thread, do so via shared data
Test the non-concurrent parts as thoroughly as you can Be extremely careful with concurrency; you have to depend much more on programming discipline, much less on testing Document your concurrency policy carefully, in order to make the program more maintainable in the future