Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.
Multithreading has several advantages over Multiprocessing such as;
• Threads are lightweight compared to processes
• Threads share the same address space and therefore can share both data and code
• Context switching between threads is usually less expensive than between processes
• Cost of thread intercommunication is relatively low that that of process intercommunication
• Threads allow different tasks to be performed concurrently.
Thread Creation
There are two ways to create thread in java;
• Implement the Runnable interface (java.lang.Runnable)
• By Extending the Thread class (java.lang.Thread)
No comments:
Post a Comment