Overview :
Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.
This is especially important for the interactive, networked environment in which Java operates, because idle time is common.
For example, the transmission rate of data over a network is much slower than the rate at which the computer can process it
The Thread Class and the Runnable Interface:
n Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable.
n · To create a new thread, your program will either extend Thread or implement the Runnable interface.
n The Thread class defines several methods that help manage threads.
Method
|
Description
|
getName
|
Obtain a thread’s name.
|
getPriority
|
Obtain a thread’s priority.
|
isAlive
|
Determine if a thread is still running.
|
join
|
Wait for a thread to terminate.
|
run
|
Entry point for the thread.
|
sleep
|
Suspend a thread for a period of time.
|
start
|
Start a thread by calling its run method.
|
stop
|
stop a thread by calling its run method.
|
The Main Thread:
When a Java program starts up, one thread begins running immediately. This is usually called the main thread of your program, because it is the one that is executed when your program begins. The main thread is important for two reasons:
• It is the thread from which other “child” threads will be spawned.
• Often, it must be the last thread to finish execution because it performs various shutdown actions.
package com.vishal.multithreading;
public class ExampleOne {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Current Thread-:"+t);
t.setName("My Thread");
System.out.println("After Change in Name-:"+t);
for(int i=5; i>0; i++){
System.out.println("I Value-:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
e.printStackTrace();
}
}
}
}
OutPut-:
Current Thread-:Thread[main,5,main]
After Change in Name-:Thread[My Thread,5,main]
I Value-:5
I Value-:6
I Value-:7
I Value-:8
I Value-:9
I Value-:10 it will keep on printing will only finish once memory leak is there.....
Creating a Thread..
(A) By extending Thread Class:
OutPut-:
Current Thread-:Thread[main,5,main]
After Change in Name-:Thread[My Thread,5,main]
I Value-:5
I Value-:6
I Value-:7
I Value-:8
I Value-:9
I Value-:10 it will keep on printing will only finish once memory leak is there.....
Creating a Thread..
(A) By extending Thread Class:
The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread.
Here is the preceding program rewritten to extend Thread:
package com.vishal.multithreading;
//create a Thread by extending thread class
public class NewThread extends Thread {
// create another thread
public NewThread() {
super("Demo Thread");
System.out.println("Child Thread:" + this);
// start the thread
start();
}
// This is entry point for the thread
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread:-:" + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child Interupted:");
}
System.out.println("Exsisting Child Thread....");
}
}
package com.vishal.multithreading;
public class FirstWayThreadClass {
public static void main(String args[]) {
// create a new thread
new NewThread();
try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread:-:" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main Interupted:");
}
System.out.println("Main thread Exsisiting.....");
}
}
Steps to refine output-:
1. The child thread is created by instantiating an object of NewThread, which is derived from Thread.
2. Notice the call to super( ) inside NewThread. This invokes the following form of the Thread constructor:
public Thread(String threadName)
Here, threadName specifies the name of the thread