Process | Thread |
---|---|
It is a computer program that is under execution. | It is the component or entity of the process that is the smallest execution unit. |
It can be divided into multiple threads. | It canβt be further subdivided. |
It has its own memory space and run-in separate memory space. | It uses the memory of the process it belongs to and run-in shared memory space.. |
These are heavy-weight operators. | These are light-weight operators. |
It requires more resources. | It requires fewer resources. |
A new thread is created.
A thread is running or is ready to run(waiting for CPU).
A thread blocked waits for a monitor lock to enter synchronized block/method.
A thread is waiting for another thread to perform a particular action.
A thread calls a method with timeout.
A thread has completed execution.
sleep()
, wait()
.Code
public interface Runnable {
public abstract void run();
}
public interface Callable<V> {
V call() throws Exception;
}
execute()
method is used to commit tasks that do not require return values, so it is impossible to judge whether the task has been executed successfully by the thread pool.submit()
method is used to submit tasks that require a return value. The thread pool will return an object of Future
type, through which you can judge whether the task is executed successfully. The return value can be obtained through the get()
method of Future.Thread.yeild()
Thread.sleep()
Thread.join()
t2.join()
Bottom logic is
wait()
.
Object.wait()
Object.notify()
or Object.notifyAll()