Threads can be created by extending the Thread Class or implementing the Runnable interface.
A thread has 4 states:
(1) NEW, created but not started.
(2) RUNNABLE, currently running, or ready to run but waiting for CPU time
(3) BLOCKED, by Thread.sleep(),IO operation,a synchronized statement or method lock.wait ( ).
(4) DEAD, when its run completes normally, or by an uncaught exception.
One Thread can wait for another to complete by calling the "join" method on that specific thread. "join" will halt the current thread till the other thread complets.
A dead thread can be started by calling the start method.
A Process is a program in execution whereas thread is a separate path of execution in a program.
What's the difference between the methods sleep() and wait()
The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and sleep() is defined in the class Thread.
How does wait and notify work?
wait() Waits indefinitely for notification. (has an overloaded (int) method.) When using the wait method you must put the condition that you are waiting on in a while for loop, This is because when the thread goes into a wait state it comes out of the synchronized block and re-enters when a notify is received. Since it is possible that some other thread has changed that condition the entry point must be where the "while(condition)" will be rechecked. If a notify is raised and nothing is waiting then the notification is lost, a subsequent wait call will have to wait for the next notify. The notifyAll method wakes up all threads waiting on the object in question. The awakened threads compete for the lock. One thread gets it, and the others go back to waiting. The Object class also defines the notify method, which arbitrarily wakes up one of the threads waiting on this object.
What is synchronization and why is it important?
With respect to multithreading, synchronization is the capability to control
the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to
significant errors.
Why would you use a synchronized block vs. synchronized method?
Synchronized blocks place locks for shorter periods than synchronized methods.
How does thread synchronization occur inside a monitor ?
To accomplish resource allocation or release, a thread calls a monitor entry (a special function or procedure that serves as an entry point into a monitor). If there is no other thread executing code within the monitor, the calling thread is allowed to enter the monitor and execute the monitor entry's code. But if a thread is already inside of the monitor, the monitor makes the calling thread wait outside of the monitor until the other thread leaves the monitor. The monitor then allows the waiting thread to enter. A synchronized method is a method that serves as a monitor entry. That method's signature is prefixed with keyword "synchronized".
What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined?- Multithreading is the mechanism in which more than one thread run independent of each other within the process. wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class. wait() : When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state. notify() or notifyAll() : To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object.
Suppose If we have variable ' I ' in run method, If I can create one or more
thread each thread will occupy a separate copy or same variable will be shared ?
Occupy a seperate copy.
Explain Thead pools.
A thread pool is a managed collection of threads that are available to perform tasks. Thread pools usually provide:
1) Improved performance when executing large numbers of tasks due to reduced per-task invocation overhead.
2) A means of bounding the resources, including threads, consumed when executing a collection of tasks.
3) To use thread pools, you instantiate an implementation of the ExecutorService interface and hand it a set of runnable tasks.
ExecutorService es=Executors.newFixedThreadPool(10);
WorkerThread[] workers = new WorkerThread[10];
for (int i = 0; i < 10; i++) {
workers[i] = new WorkerThread(i);
es.execute(workers[i]);
}
es.shutdown();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment