Java Examples
OOPS |
Exception |
RMI |
String |
Thread |
io |
Util |
Net |
AWT |
JDBC |
|
|
| THREAD PROGRAM:: |
| 1.Thread Extending Program |
//Thread by Extending the Thread Class ;
class NewThread extends Thread {
NewThread(){
super("Demo Thread");
System.out.println("Child Thread: " + this);
start();
}
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("Exiting child thread");
}
}
}
public class ExtThread {
public static void main(String args[]) {
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 thread interrupted");
}
System.out.println("Main thread exiting");
}
}
|
| 2.Main Thread Program |
public class MainThread {
public static void main(String args[]) {
Thread t=Thread.currentThread();
System.out.println("Current thread: " + t);
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n=5;n>0;n--) {
System.out.println(n);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
System.out.println("Main thread interrupted");
}
System.out.println("Main Thread Exits : ");
}
}
|
| 3.Multi Thread Program |
class NewThread implements Runnable {
String name;
Thread t;
NewThread(String threadname) {
name = threadname;
t=new Thread(this,name);
System.out.println("New thread: " + t);
t.start();
}
public void run(){
try {
for(int i=5;i>0;i--) {
System.out.println(name + ":" +i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println(name + "Interrupted");
}
System.out.println(name + "exiting");
}
}
public class MultiThread {
public static void main(String args[]){
new NewThread("One");
new NewThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
}catch(InterruptedException e){
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting");
}
}
|
| 4.User Defined Exception |
class NewThread implements Runnable {
String name;
Thread t;
NewThread(String threadname) {
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: " + t);
t.start();
}
public void run() {
try {
for(int i=5;i>0;i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println(name + " interrupted.");
}
System.out.println(name + " exitiing.");
}
}
public class JoinDemo {
public static void main(String args[]) {
NewThread ob1=new NewThread("One");
NewThread ob2=new NewThread("Two");
NewThread ob3=new NewThread("Three");
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: " + ob2.t.isAlive());
System.out.println("Thread Three is alive: " + ob3.t.isAlive());
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e){
System.out.println("Main Thread Interrupted");
}
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: " + ob2.t.isAlive());
System.out.println("Thread Three is alive: " + ob3.t.isAlive());
System.out.println("Main Thread exiting");
}
}
|
|
Back to Top |