08-10-2016, 11:16 AM
1458318384-ThreadProgramminginJava.pptx (Size: 196.72 KB / Downloads: 7)
Thread
“Thread is a lightweight process.”
Process : “Program in execution”
A processor executes threads, not processes.
So each application has at least one process, and a process always has at least one thread of execution, known as the primary thread.
We can create or assign more than one thread for a single process.
Multithreading : ”It is the ability of an operating system to execute different parts of a program, i.e. threads, on a single processor concurrently.”
It enables maximum use of the CPU, because idle time can be kept to a minimum.
A multithreading is a specialized form of multitasking.
Example: Word Document
Thread life cycle
Creating a Thread
Java defines two ways in which this can be accomplished:
Implement the Runnable interface
Extend the Thread class, itself.
Program:1
Write an application that executes two threads. One thread display “Hello world” every 2000 milliseconds and another thread display “How are you?” every 1000 milliseconds. Create the threads by implementing Runnable interface.