Multithreading

five thread states: NEW A thread that has not yet started is in this state. RUNNABLE A thread executing in the Java virtual machine is in this state. BLOCKED A thread that is blocked waiting for a monitor lock is in this state. WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state. TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. TERMINATED A thread that has exited is in this state.

Two ways to create a thread

  1. extend Thread class.
  2. implementing Runnable interface

  3. extend Thread class: (1) override run() method. put my complete business logic inside this method. (2) execute start() method. This method calls run() method.

Don't override start() method.

Since a class can only inherit one class, implementing Runnable interface is provided.

  1. implementing Runnable interface: (1) implement a run() method provided by Runnable interface. (2) instantiate a Thread object using the following constructor: Thread(Runnable threadObj, String threadName);

Process and thread

Both processes and threads are independent sequences of execution.

Thread is called lightweight process. Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. Threads share the same address space.

A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.

Concurrency

The "Synchronized" keywords prevents concurrent access to a block of code or object by multiple Threads. Synchronization is the capability to control the access of multiple threads to shared resources. synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.

difference between sleep() and yield():

  1. yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution (yield does not run).
  2. Thread.sleep() will cause currently executing thread to stop execution and relinquish the CPU to allow Thread scheduler to allocate CPU to another thread or same thread depends upon Thread scheduler.
  3. yield() -> ready state. sleep() -> waiting state.

放弃CPU并不代表释放了线程的锁:Sleep as well as Yield is used to relinquish CPU from current thread, but at same time it doesn't release any lock held by the thread.

http://java67.blogspot.com/2012/08/difference-between-yield-and-sleep-in.html

Synchronization

Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. (The API specification often refers to this entity simply as a "monitor.") Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.

Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.

When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquisition of the same lock.

An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock.

Interthread Communication

public void wait() : Causes the current thread to wait until another thread invokes the notify().

public void notify() : Wakes up a single thread that is waiting on this object's monitor. The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.

public void notifyAll() : Wakes up all the threads that called wait( ) on the same object.

results matching ""

    No results matching ""