-
Notifications
You must be signed in to change notification settings - Fork 0
multi hreading
Keyhan Hadjari edited this page Sep 3, 2016
·
6 revisions
- Synchronized: makes methods or block of code thread safe by locking them when called by one thread, the lock is removed when call has finished.
- volatile: like synchronized but for variables and only for reading (lazy read), indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. If volatile is not used then you cannot be sure that the value is read is the current value, could be cached.
- Method threadX.join() method makes the current thread to wait for threadX finish and then continue.
- A thread becomes the owner of the object's monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a synchronized statement that synchronizes on the object.
- For objects of type Class, by executing a synchronized static method of that class.
- The java.lang.Thread.yield() method causes the currently executing thread object to temporarily pause and allow other threads to execute.
- The java.lang.Thread.start() method causes this thread to begin execution, the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: The current thread (which returns from the call to the start method) and the other thread (which executes its run method).
- Method stop() is called to stop a thread
- If a thread with higher priority becomes runable (ready) then current thread is stopped.
- In a synchronized block thread.sleep() still under lock ,object.wait() the lock is removed
- The start method cannot be used repeatedly, if done then IllegalThreadStateException is thrown.
- The java.lang.Object.notify() wakes up a single thread that is waiting on this object's monitor.
- If any threads are waiting on this object, one of them is chosen to be awakened.
- The choice is arbitrary and occurs at the discretion of the implementation.
- A thread waits on an object's monitor by calling one of the wait methods.
- This method should only be called by a thread that is the owner of this object's monitor.
- Method notifyAll() is called from synchronized block
- To call wait() the thread must own its lock on the object
- A wait can be "woken up" by another process calling notify on the monitor which is being waited on whereas a sleep cannot. Also a wait (and notify) must be in a block synchronized on the monitor object whereas sleep does not. Another point is that you call wait on Object itself (you wait on an object's monitor) whereas you call sleep on Thread.
- If programmer want to use several threads then he have to use StringBuffer
- StringBuffer is thread-safe, mutable, synchronized sequence of characters
- StringBuilder is not synchronized but mutable.
- String class is immutable, StringBuffer and StringBuilder are mutable.
- Immutable classes are classes where its objects can not be modified, for example an object of type String cannot be changed (by its methods) it can only be replaced.